您的位置:首页 > 其它

POJ - 3414 Pots(15.10.10 搜索专题)bfs

2015-10-11 22:24 726 查看
Pots

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
Submit Status

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
DROP(i)      empty the pot i to the drain;
POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its
contents have been moved to the potj).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the
desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4


Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)


思路:简单题,倒水瓶游戏,挺好玩的题目,直接模拟操作然后bfs就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<stack>
using namespace std;

const string tip[8] = { "", "DROP(1)", "DROP(2)", "FILL(1)", "FILL(2)", "POUR(1,2)", "POUR(2,1)" };

int A, B, C;

struct node
{
int inA, inB;
int step;
node(int a, int b, int s)
{
inA = a;
inB = b;
step = s;
}
};

struct Op
{
int opnum;
int prea,preb;
Op() { prea = -1; preb = -1;}
}op[105][105];

int lastA, lastB;

bool vis[105][105];

void pourAtoB(int &a, int &b)
{
int yul = B - b;
if (a <= yul)
{
b += a;
a = 0;
}
else
{
b = B;
a = a - yul;
}
}

void pourBtoA(int &a, int &b)
{
int yul = A - a;
if (b <= yul)
{
a += b;
b = 0;
}
else
{
a = A;
b = b - yul;
}
}

int bfs()
{
queue<node>que;
que.push(node(0, 0, 0));
vis[0][0] = true;
while (!que.empty())
{
node loc = que.front(); que.pop();
if (loc.inA == C || loc.inB==C)
{
lastA = loc.inA, lastB = loc.inB;
return loc.step;
}
for (int i = 1; i <= 6; i++)
{
if (i == 1)		//drop A
{
int nA = 0;
int nB = loc.inB;
if (!vis[nA][nB])
{
que.push(node(nA, nB, loc.step + 1));
vis[nA][nB] = true;

op[nA][nB].prea = loc.inA;
op[nA][nB].preb = loc.inB;
op[nA][nB].opnum = 1;
}
}
else if (i == 2)	 //drop B
{
int nA = loc.inA;
int nB = 0;
if (!vis[nA][nB])
{
que.push(node(nA, nB, loc.step + 1));
vis[nA][nB] = true;

op[nA][nB].prea = loc.inA;
op[nA][nB].preb = loc.inB;
op[nA][nB].opnum = 2;
}
}
else if (i == 3)	//fill A
{
int nA = A;
int nB = loc.inB;
if (!vis[nA][nB])
{
que.push(node(nA, nB, loc.step + 1));
vis[nA][nB] = true;

op[nA][nB].prea = loc.inA;
op[nA][nB].preb = loc.inB;
op[nA][nB].opnum = 3;
}
}
else if (i == 4)	//fill B
{
int nA = loc.inA;
int nB = B;
if (!vis[nA][nB])
{
que.push(node(nA, nB, loc.step + 1));
vis[nA][nB] = true;

op[nA][nB].prea = loc.inA;
op[nA][nB].preb = loc.inB;
op[nA][nB].opnum = 4;
}
}
else if (i == 5)    //pour A to B
{
int nA = loc.inA;
int nB = loc.inB;
pourAtoB(nA, nB);
if (!vis[nA][nB])
{
que.push(node(nA, nB, loc.step + 1));
vis[nA][nB] = true;

op[nA][nB].prea = loc.inA;
op[nA][nB].preb = loc.inB;
op[nA][nB].opnum = 5;
}
}
else if (i == 6)    //pour B to A
{
int nA = loc.inA;
int nB = loc.inB;
pourBtoA(nA, nB);
if (!vis[nA][nB])
{
que.push(node(nA, nB, loc.step + 1));
vis[nA][nB] = true;

op[nA][nB].prea = loc.inA;
op[nA][nB].preb = loc.inB;
op[nA][nB].opnum = 6;
}
}
}
}
return -1;
}

int main()
{
while (scanf("%d%d%d", &A, &B, &C) != EOF)
{
memset(vis, false, sizeof(vis));
for (int i = 0; i <= 100;i++)
for (int j = 0; j <= 100; j++)
op[i][j].prea = op[i][j].preb = -1;

int ans = bfs();

if (ans == -1)
{
cout << "impossible" << endl;
continue;
}

cout << ans << endl;
stack<int>ansop;
ansop.push(op[lastA][lastB].opnum);
while (1)
{
int ta = op[lastA][lastB].prea;
int tb = op[lastA][lastB].preb;
if (op[ta][tb].prea == -1) break;
ansop.push(op[ta][tb].opnum);
lastA = ta;
lastB = tb;
}

int c = 1;
while (!ansop.empty())
{
cout << tip[ansop.top()] << endl;
ansop.pop();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 搜索 bfs