标签:
#include<algorithm> #include<stdio.h> #include<string.h> #include<queue> #include<stdlib.h> using namespace std; int A, B, C; char a[10][10] ={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"}; #define maxn 510 #define INF 0x3f3f3f3f struct point { int x,y; }; struct node { int fromx,fromy,step,op; }v[maxn][maxn]; void dfs(int x,int y) { if(x==0&&y==0) return ; dfs(v[x][y].fromx,v[x][y].fromy); printf("%s\n",a[v[x][y].op]); } void bfs() { queue<point>Q; point s,q; s.x = s.y = 0; v[0][0].step = 1; Q.push(s); while(Q.size()) { s = Q.front(); Q.pop(); if(s.x==C||s.y==C) { printf("%d\n",v[s.x][s.y].step-1); dfs(s.x,s.y); return ; } for(int i=0;i<6;i++) { q = s; if(i == 0) q.x = A; else if(i == 1) q.y = B; else if(i == 2) q.x = 0; else if(i == 3) q.y = 0; else if(i == 4) { if(q.x+q.y <= B) q.y += q.x, q.x = 0;//把A里面的水全倒B里面 else q.x -= (B-q.y), q.y = B;//把B倒满 } else { if(q.x+q.y <= A) q.x += q.y, q.y = 0; else q.y -= (A-q.x), q.x = A; } if(v[q.x][q.y].step == 0) { v[q.x][q.y].step = v[s.x][s.y].step+1; v[q.x][q.y].fromx = s.x; v[q.x][q.y].fromy = s.y; v[q.x][q.y].op = i; Q.push(q); } } } printf("impossible\n"); } int main() { while(scanf("%d%d%d", &A,&B,&C)!=EOF) { memset(v,0,sizeof(v)); bfs(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/biu-biu-biu-/p/5761450.html