Time Limit: 2 Seconds Memory Limit: 65536 KB
In the movie “Die Hard 3”, Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.
You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.
A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are
fill A
fill B
empty A
empty B
pour A B
pour B A
success
where “pour A B” means “pour the contents of jug A into jug B”, and “success” means that the goal has been accomplished.
You may assume that the input you are given does have a solution.
Input
Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.
Output
Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line “success”. Output lines start in column 1 and there should be no empty lines nor any trailing spaces.
Sample Input
3 5 4
5 7 3
Sample Output
fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
题目链接:ZOJ-1005
题目大意:类似于POJ-3414,详见 POJ-3414题解
以下是代码:
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
int a,b,c;
struct node
{
int a,b;
int cnt;
};
node init(int num1,int num2,int num3)
{
node tmp;
tmp.a = num1;
tmp.b = num2;
tmp.cnt = num3;
return tmp;
}
node father[105][105];
int vis[1005][1005];
queue <node> que;
string s[100];
void printf_ans(node ans)
{
int k = 0;
while(1)
{
if (ans.a == 0 && ans.b == 0) break;
node pre = father[ans.a][ans.b];
if (ans.b == pre.b) //b 没有改变
{
if (ans.a == 0) s[k++] = "empty A";
else s[k++] = "fill A";
}
else if (ans.a == pre.a)
{
if (ans.b == 0) s[k++] = "empty B";
else s[k++] = "fill B";
}
else if ((ans.a == 0 && ans.b != pre.b) || (ans.a != pre.a && ans.b == b)) //把a倒进b
{
s[k++] = "pour A B";
}
else
{
s[k++] = "pour B A";
}
ans.a = pre.a;
ans.b = pre.b;
}
for (int i = k - 1; i >= 0; i--)
{
cout << s[i] << endl;
}
cout << "success\n";
}
void bfs()
{
node zero = init(0,0,0);
que.push(zero);
vis[0][0] = 1;
while(!que.empty())
{
node front = que.front();
que.pop();
node tmp;
if (front.a == c || front.b == c)
{
printf_ans(front);
return;
}
if (front.a != 0 && !vis[0][front.b]) //清空a操作
{
tmp = init(0,front.b,front.cnt + 1);
que.push(tmp);
vis[0][front.b] = 1;
father[0][front.b] = front;
}
if (front.b != 0 && !vis[front.a][0]) //清空b操作
{
tmp = init(front.a,0,front.cnt + 1);
que.push(tmp);
vis[front.a][0] = 1;
father[front.a][0] = front;
}
if (front.a != a && !vis[a][front.b]) //倒满a
{
tmp = init(a,front.b,front.cnt + 1);
que.push(tmp);
vis[a][front.b] = 1;
father[a][front.b] = front;
}
if (front.b != b && !vis[front.a][b]) //倒满b
{
tmp = init(front.a,b,front.cnt + 1);
que.push(tmp);
vis[front.a][b] = 1;
father[front.a][b] = front;
}
if (front.a > 0 && front.b < b) //a倒给b
{
int need = b - front.b;
if (front.a > need && !vis[front.a - need][b]) //b满
{
tmp = init(front.a - need,b,front.cnt + 1);
que.push(tmp);
vis[front.a - need][b] = 1;
father[front.a - need][b] = front;
}
else if (!vis[0][front.b + front.a]) //a倒完
{
tmp = init(0,front.b + front.a,front.cnt + 1);
que.push(tmp);
vis[0][front.b + front.a] = 1;
father[0][front.b + front.a] = front;
}
}
if (front.b > 0 && front.a < a) //b倒给a
{
int need = a - front.a;
if (front.b > need && !vis[a][front.b - need]) //a满
{
tmp = init(a,front.b - need,front.cnt + 1);
que.push(tmp);
vis[a][front.b - need] = 1;
father[a][front.b - need] = front;
}
else if (!vis[front.a + front.b][0])//b倒完
{
tmp = init(front.a + front.b,0,front.cnt + 1);
que.push(tmp);
vis[front.a + front.b][0] = 1;
father[front.a + front.b][0] = front;
}
}
}
// cout << "impossible\n";
}
int main()
{
while(cin >> a >> b >> c)
{
while (!que.empty()) {
que.pop();
}
memset(vis, 0, sizeof(vis));
memset(father,0,sizeof(father));
bfs();
}
return 0;
}