【題目描述】
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)
【題目分析】
是一道很裸的BFS,就是稍微復雜一點,在做過非常可樂(也是一道BFS)后,這道題簡直一模一樣,就是稍微復雜一點點,注意細節
還有就是題目要求輸出步驟,這就需要保存一下路徑,我用了一個vector(發現STL好好用)
【AC代碼】
#include<cstdio>
#include<cstring>
#include<queue>
#include<climits>
#include<vector>using namespace std;const int MAXN=105;
int A,B,C;
int check[MAXN][MAXN];
struct node1
{int x; int a;
}tt;
struct node
{int a,b;vector<node1> path;int step;
}t,p,ans;bool BFS()
{queue<node> q;p.a=0; p.b=0; p.path.clear(); p.step=0;q.push(p);memset(check,-1,sizeof(check));check[0][0]=0;while(!q.empty()){p=q.front(); q.pop();if(p.a==C || p.b==C){ans=p;return true;}for(int i=1;i<=3;i++){if(i==1){for(int j=1;j<=2;j++){if(j==1 && p.a<A){t=p;t.a=A;if(check[t.a][t.b]!=-1) continue;tt.x=1; tt.a=1;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 && p.b<B){t=p;t.b=B;if(check[t.a][t.b]!=-1) continue;tt.x=1; tt.a=2;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}else if(i==2){for(int j=1;j<=2;j++){if(j==1 && p.a>0){t=p;t.a=0;if(check[t.a][t.b]!=-1) continue;tt.x=2; tt.a=j;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 &&p.b>0){t=p;t.b=0;if(check[t.a][t.b]!=-1) continue;tt.x=2; tt.a=j;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}else if(i==3){for(int j=1;j<=2;j++){if(j==1 && p.a>0 && p.b<B){t=p;if(p.a>=B-p.b){t.a=t.a+t.b-B;t.b=B;}else{t.b+=t.a;t.a=0;}if(check[t.a][t.b]!=-1) continue;tt.x=3; tt.a=1;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 && p.b>0 && p.a<A){t=p;if(p.b>=A-p.a){t.b=t.a+t.b-A;t.a=A;}else{t.a+=t.b;t.b=0;}if(check[t.a][t.b]!=-1) continue;tt.x=3; tt.a=2;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}}}return false;
}int main()
{scanf("%d%d%d",&A,&B,&C);if(BFS()){printf("%d\n",ans.step);for(int i=0,j=ans.step;i<j;i++){if(ans.path[i].x==1){printf("FILL(%d)\n",ans.path[i].a);}else if(ans.path[i].x==2){printf("DROP(%d)\n",ans.path[i].a);}else if(ans.path[i].x==3){int tmp=(ans.path[i].a==1)?2:1;printf("POUR(%d,%d)\n",ans.path[i].a,tmp);}}}else{printf("impossible");}return 0;
}