#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack > #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> Using namespace Std;struct node{int x, y;}; int A,b,tar;node prestate[1010][1010];int ope[10101][1010];int flag[1010][1010];void Print (node cur) {if (cur.x = = 0 && Cur.y = = 0) Return;int op = ope[cur.x][cur.y];node s = prestate[cur.x][cur.y];p rint (s); switch (OP) {case 1:{ printf ("Fill a\n"); Case 2:{printf ("Fill b\n"); Case 3:{printf ("pour A b\n"); Case 4:{printf ("pour B a\n"); Case 5:{printf ("Empty a\n"); Case 6:{printf ("Empty b\n"); void BFs (int x,int y) {node s;s.x = X;s.y = Y;queue<node > Que;que.push (s); memset (flag,0,sizeof (flag)); flag[s.x][ S.Y] = 1;node v;int tag = 1;while (!que.empty ()) {node TMP = Que.front (); Que.pop ();//fill av.x = A;v.y = Tmp.y;if (!flag[v.x] [V.Y]) {Que.push (v); FLAG[V.X][V.Y] = 1;ope[v.x][v.y] = 1; PRESTATE[V.X][V.Y] = tmp;} if (v.x = = Tar | | v.y = = TAR) {tag = 0;break;} Fill bv.x = Tmp.x;v.y = B;if (!flag[v.x][v.y]) {Que.push (v); FLAG[V.X][V.Y] = 1;ope[v.x][v.y] = 2; PRESTATE[V.X][V.Y] = tmp ;} if (v.x = = Tar | | v.y = = TAR) {tag = 0;break;} Pour a bv.x = tmp.x-min (tmp.x,b-tmp.y), v.y = tmp.y + min (tmp.x, b-tmp.y), if (!flag[v.x][v.y]) {Que.push (v); flag[v.x ][V.Y] = 1;ope[v.x][v.y] = 3; PRESTATE[V.X][V.Y] = tmp;} if (v.x = = Tar | | v.y = = TAR) {tag = 0;break;} Pour b-av.x = tmp.x + min (tmp.y, a-tmp.x), v.y = Tmp.y-min (tmp.y,a-tmp.x), if (!flag[v.x][v.y]) {Que.push (v); flag[v. X][V.Y] = 1;ope[v.x][v.y] = 4; PRESTATE[V.X][V.Y] = tmp;} if (v.x = = Tar | | v.y = = TAR) {tag = 0;break;} EMP av.x = 0;V.Y = Tmp.y;if (!flag[v.x][v.y]) {Que.push (v); FLAG[V.X][V.Y] = 1;ope[v.x][v.y] = 5; PRESTATE[V.X][V.Y] = tmp; }if (v.x = = Tar | | v.y = tar) {tag = 0;break;} EMP bv.x = TMP.X;V.Y = 0;if (!flag[v.x][v.y]) {Que.push (v); FLAG[V.X][V.Y] = 1;ope[v.x][v.y] = 6; PRESTATE[V.X][V.Y] = tmp; }if (v.x = = Tar | | v.y = = TAR) {tag = 0;break;}} if (!tag) {print (v);p rintf ("success\n");} else{printf ("unsuccess\n");}} int main () {while (scanf ("%d%d%d", &a,&b,&tar)!=eof) {BFS (0,0);} return 0;}
An implicit graph search problem for inverted water problem