跟"非常可乐"那题差不多,只是多了一个打印倒水步骤。
View Code
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #define N 1001 5 #define MIN(a,b) ((a)<(b)?(a):(b)) 6 using namespace std; 7 typedef struct node 8 { 9 int v[2],t,d; 10 }node; 11 node cur,next; 12 queue<node> Q; 13 node p[N][N]; 14 int c[2],x; 15 char vis[N][N]; 16 char s[6][10]={"fill A","fill B","empty A","empty B","pour A B","pour B A"}; 17 node st_tran(node tmp,int i) 18 { 19 int d; 20 switch(i) 21 { 22 case 0: tmp.v[0]=c[0]; break; 23 case 1: tmp.v[1]=c[1]; break; 24 case 2: tmp.v[0]=0; break; 25 case 3: tmp.v[1]=0; break; 26 case 4: d=MIN(tmp.v[0],c[1]-tmp.v[1]),tmp.v[0]-=d,tmp.v[1]+=d; break; 27 case 5: d=MIN(tmp.v[1],c[0]-tmp.v[0]),tmp.v[1]-=d,tmp.v[0]+=d; break; 28 } 29 tmp.t++; 30 tmp.d=i; 31 return tmp; 32 } 33 void print(node tmp) 34 { 35 if(tmp.d==-1) return; 36 else 37 { 38 print(p[tmp.v[0]][tmp.v[1]]); 39 puts(s[tmp.d]); 40 } 41 } 42 void bfs() 43 { 44 bool success=false; 45 memset(vis,0,sizeof(vis)); 46 cur.v[0]=0; 47 cur.v[1]=0; 48 cur.t=0; 49 cur.d=-1; 50 while(!Q.empty()) Q.pop(); 51 vis[0][0]=1; 52 Q.push(cur); 53 while(!Q.empty() && !success) 54 { 55 cur=Q.front(),Q.pop(); 56 if(cur.v[1]==x) success=true,print(cur); 57 for(int i=0;!success && i<6;i++) 58 { 59 next=st_tran(cur,i); 60 if(vis[next.v[0]][next.v[1]]) continue; 61 vis[next.v[0]][next.v[1]]=1; 62 p[next.v[0]][next.v[1]]=cur; 63 if(next.v[1]==x) success=true,print(next); 64 else Q.push(next); 65 } 66 } 67 if(success) puts("success"); 68 } 69 int main() 70 { 71 while(~scanf("%d%d%d",&c[0],&c[1],&x)) 72 { 73 bfs(); 74 } 75 return 0; 76 }