题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495
每次六种决策,见注释。
vis开二维就够用了,因为水的总量是一定的
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 const int maxn=110; 6 int vis[maxn][maxn][maxn]; 7 int ca,cb,cc; 8 int ban; 9 struct staus 10 { 11 int a,b,c; 12 int dis; 13 }now,nex; 14 15 int bfs() 16 { 17 queue<staus> q; 18 now.a=ca;now.b=0;now.c=0; 19 now.dis=0; 20 vis[ca][0][0]=1; 21 q.push(now); 22 while(!q.empty()) 23 { 24 now=q.front(); 25 q.pop(); 26 if(now.a==ban&&(now.b==ban||now.c==ban)||now.b==ban&&(now.a==ban||now.c==ban) 27 ||now.c==ban&&(now.b==ban||now.a==ban)) return now.dis; 28 if(now.a>0&&now.b<cb) //a倒给b 29 { 30 int v=min(now.a,cb-now.b); 31 nex.a=now.a-v; 32 nex.b=now.b+v; 33 nex.c=now.c; 34 if(!vis[nex.a][nex.b][nex.c]) { 35 vis[nex.a][nex.b][nex.c]=1; 36 nex.dis=now.dis+1; 37 q.push(nex); 38 } 39 } 40 if(now.a>0&&now.c<cc) //a倒给c 41 { 42 int v=min(now.a,cc-now.c); 43 nex.a=now.a-v; 44 nex.c=now.c+v; 45 nex.b=now.b; 46 if(!vis[nex.a][nex.b][nex.c]) { 47 vis[nex.a][nex.b][nex.c]=1; 48 nex.dis=now.dis+1; 49 q.push(nex); 50 } 51 } 52 if(now.a<ca&&now.b>0) //b倒给a 53 { 54 int v=min(now.b,ca-now.a); 55 nex.a=now.a+v; 56 nex.b=now.b-v; 57 nex.c=now.c; 58 if(!vis[nex.a][nex.b][nex.c]) { 59 vis[nex.a][nex.b][nex.c]=1; 60 nex.dis=now.dis+1; 61 q.push(nex); 62 } 63 } 64 if(now.a<ca&&now.c>0) //c倒给a 65 { 66 int v=min(now.c,ca-now.a); 67 nex.c=now.c-v; 68 nex.a=now.a+v; 69 nex.b=now.b; 70 if(!vis[nex.a][nex.b][nex.c]) { 71 vis[nex.a][nex.b][nex.c]=1; 72 nex.dis=now.dis+1; 73 q.push(nex); 74 } 75 } 76 if(now.c>0&&now.b<cb) //c倒给b 77 { 78 int v=min(now.c,cb-now.b); 79 nex.c=now.c-v; 80 nex.b=now.b+v; 81 nex.a=now.a; 82 if(!vis[nex.a][nex.b][nex.c]) { 83 vis[nex.a][nex.b][nex.c]=1; 84 nex.dis=now.dis+1; 85 q.push(nex); 86 } 87 } 88 if(now.b>0&&now.c<cc) //b倒给c 89 { 90 int v=min(now.b,cc-now.c); 91 nex.c=now.c+v; 92 nex.b=now.b-v; 93 nex.a=now.a; 94 if(!vis[nex.a][nex.b][nex.c]) { 95 vis[nex.a][nex.b][nex.c]=1; 96 nex.dis=now.dis+1; 97 q.push(nex); 98 } 99 } 100 101 } 102 return -1; 103 104 } 105 int main() 106 { 107 while(scanf("%d%d%d",&ca,&cb,&cc)&&(ca||cb||cc)) 108 { 109 if(ca%2) {puts("NO");continue;} 110 ban=ca/2; 111 memset(vis,0,sizeof(vis)); 112 int st=bfs(); 113 if(st!=-1) printf("%d ",st); 114 else puts("NO"); 115 } 116 }