Pots Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ You are given two pots, having the volume of A and B liters respectively. the following operations can be saved med: 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. 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
Message: Fill (2) Pour () drop (1) Pour () Fill (2) Pour)
For two empty bottles, the volume is a B. There are 6 operations to clear a (or B), fill a (or B), and pour a into B, add B to. Corresponding to these 6 operations, there are 6 statuses, typical BFS search. Not much, but the answer to this question is clearly the answer to a single group of input results, but only multiple groups of input are required, contributing 5 wa in vain.
#include <cstdio>#include <iostream>#include <cstring>#include <cstdlib>#include <queue>using namespace std;int m,n,c;typedef struct node{int v1,v2,op;};bool vis[999][999];void bfs(){node t={0,0,0};queue <node> Q;Q.push(t);vis[0][0]=1;while(!Q.empty()){node f=Q.front();Q.pop();if(f.v1==c||f.v2==c){cout<<f.op<<endl;return ;}if(f.v1!=m){t.v1=m;t.op=f.op+1;t.v2=f.v2;if(!vis[t.v1][t.v2]){ vis[t.v1][t.v2]=1; Q.push(t);}}if(f.v2!=n){t.v2=n;t.op=f.op+1;t.v1=f.v1;if(!vis[t.v1][t.v2]){ vis[t.v1][t.v2]=1; Q.push(t);}}if(f.v1!=0){t.v1=0;t.v2=f.v2;t.op=f.op+1;if(!vis[t.v1][t.v2]){ vis[t.v1][t.v2]=1; Q.push(t);}}if(f.v2!=0){t.v2=0;t.v1=f.v1;t.op=f.op+1;if(!vis[t.v1][t.v2]){ vis[t.v1][t.v2]=1; Q.push(t);}}if(f.v2!=0&&f.v1!=m){t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;t.v1=f.v1+f.v2; if(t.v1>m) t.v1=m;t.op=f.op+1;if(!vis[t.v1][t.v2]){ vis[t.v1][t.v2]=1; Q.push(t);}}if(f.v1!=0&&f.v2!=n){t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;t.v2=f.v2+f.v1; if(t.v2>n) t.v2=n;t.op=f.op+1;if(!vis[t.v1][t.v2]){ vis[t.v1][t.v2]=1; Q.push(t);}}}puts("impossible");}int main(){while(cin>>m>>n>>c){memset(vis,0,sizeof(vis));bfs();}return 0;}