http://poj.org/problem?id=3414
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 ≤ i ≤ 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 pot j).
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 A, B, 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)
六种状态
FILL(1)//将1号容器充满
FILL(2)//将2号容器充满
DROP(1)//将1号容器的水全部倒出
DROP(2)//将2号容器的水全部倒出
POUR(1,2)//将1号容器的水倒入2号容器,若不足则倒完,否则,将2号容器倒满为止
POUR(2,1)//将2号容器的水倒入1号容器,若不足则倒完,否则,将1号容器倒满为止
pots[][]记录路径,简单的BFS就OK了
用C++交一直RE,最后改了G++就AC了
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
#define N 106
#define met(a, b) memset (a, b, sizeof (a))
typedef long long LL;
//const int INF = ((1<<31)-1);
struct node1
{
int prea, preb, op;
}pots[N][N];
struct node
{
int a, b, step;
bool friend operator < (const node &a, const node &b)
{
return a.step > b.step;
}
}p, q;
int a, b, c, flag, path[N*20000], vis[N][N];
node BFS ()
{
priority_queue <node> que;
p.a = p.b = p.step = 0;
pots[0][0].prea = pots[0][0].preb = pots[0][0].op = 0;
met (vis, 0);
vis[0][0] = 1;
que.push(p);
while (que.size())
{
q = que.top(); que.pop();
if (q.a == c || q.b == c)
{
flag = 1;
return q;
}
for (int i=1; i<=6; i++)
{
if (i == 1) p.a = a, p.b = q.b;//FILL(1);
if (i == 2) p.a = q.a, p.b = b;//FILL(2);
if (i == 3) p.a = 0, p.b = q.b;//DROP(1);
if (i == 4) p.a = q.a, p.b = 0;//DROP(2);
if (i == 5)//POUR(1,2);
{
if (q.a+q.b > b) p.a=q.a-(b-q.b), p.b=b;
else p.a=0, p.b=q.b+q.a;
}
if (i == 6)//POUR(2,1);
{
if (q.a+q.b > a) p.a=a, p.b=q.b-(a-q.a);
else p.a=q.a+q.b, p.b=0;
}
if (!vis[p.a][p.b])
{
vis[p.a][p.b] = 1;
pots[p.a][p.b].prea = q.a;
pots[p.a][p.b].preb = q.b;
pots[p.a][p.b].op = i;
p.step = q.step + 1;
que.push (p);
}
}
}
}
int main ()
{
node ans;
while (scanf ("%d %d %d", &a, &b, &c) != EOF)
{
met (path, 0);
flag = 0;
ans = BFS ();
if (!flag)
{
puts ("impossible");
continue;
}
int step = ans.step, x = ans.a, y = ans.b;
printf ("%d\n", step);
for (int i=step; i>=1; i--)
{
path[i] = pots[x][y].op;
int xx = x;
x = pots[xx][y].prea;
y = pots[xx][y].preb;
}
for (int i=1; i<=step; i++)
{
if (path[i] == 1) puts ("FILL(1)");
if (path[i] == 2) puts ("FILL(2)");
if (path[i] == 3) puts ("DROP(1)");
if (path[i] == 4) puts ("DROP(2)");
if (path[i] == 5) puts ("POUR(1,2)");
if (path[i] == 6) puts ("POUR(2,1)");
}
}
return 0;
}