# POJ1606总结

## Jugs

### Description

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 

### 我的代码

#include<stdio.h>
#include<vector>
#include<stack>
#include<string.h>

using namespace std;

//定义操作步骤
const int fullA=1,fullB=2,emptyA=3,emptyB=4,pourAtoB=5,pourBtoA=6;
const char steps[10][10]={"success","fill A","fill B","empty A","empty B","pour A B","pour B A"};

int a,b,n;
int flag [1000][1000];

struct node{
int a,b,now,pre;//a，b是两个水壶当前水的体积，now是这一步的操作步骤，pre是上一步的索引
node(){}
node(int a,int b,int now,int pre):a(a),b(b),now(now),pre(pre){}
};
int main()
{
while (scanf("%d%d%d",&a,&b,&n)==3){
if (n==0){
printf("%s\n%s\n",steps[emptyB],steps[0]);
continue;
}
memset(flag,0,sizeof(flag));
int sum,tempa,tempb;
int end;
node now;
vector<node> v;
stack<int> s;
v.push_back(node(0,0,0,-1));
flag[0][0]=1;
//fullA
if (flag[a][now.b]==0){
flag[a][now.b]=1;
tail++;
}
//fullB
if (flag[now.a][b]==0){
flag[now.a][b]=1;
if ((v[tail]).b==n){
end =tail;
break;
}
tail++;
}
//emptyA
if (flag[0][now.b]==0){
flag[0][now.b]=1;
tail++;
}
//emptyB
if (flag[0][now.b]==0){
flag[now.a][0]=1;
tail++;
}
sum=now.a+now.b;
//pourAtoB
{
if (sum>b){
tempb=b;
tempa=sum-b;
}else{
tempb=sum;
tempa=0;
}
if (flag[tempa][tempb]==0){
flag[tempa][tempb]=1;
if ((v[tail]).b==n){
end =tail;
break;
}
tail++;
}
}
//pourBtoA
{
if (sum>a){
tempa=a;
tempb=sum-a;
}else{
tempa=sum;
tempb=0;
}
if (flag[tempa][tempb]==0){
flag[tempa][tempb]=1;
if ((v[tail]).b==n){
end =tail;
break;
}
tail++;
}
}
}

while((v[end]).pre!=-1){
s.push((v[end]).now);
end=(v[end]).pre;
}
while(!s.empty()){
printf("%s\n",steps[s.top()]);
s.pop();
}
printf("%s\n",steps[0]);
}
return 0;
}