标签:dfs
#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]; print(s); switch(op){ case 1:{ printf("fill A\n"); break; } case 2:{ printf("fill B\n"); break; } case 3:{ printf("pour A B\n"); break; } case 4:{ printf("pour B A\n"); break; } case 5:{ printf("empty A\n"); break; } case 6:{ printf("empty B\n"); break; } } } 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 a v.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 b v.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 b v.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 - a v.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 a v.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 b v.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); printf("success\n"); } else{ printf("unsuccess\n"); } } int main(){ while(scanf("%d%d%d",&a,&b,&tar)!=EOF){ bfs(0,0); } return 0; }
标签:dfs
原文地址:http://blog.csdn.net/qq_24667639/article/details/45267573