链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1495
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7620 Accepted Submission(s): 3050
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> using namespace std; #define maxn 110 struct node { int a, b, c, step; }; int S, N, M, vis[maxn][maxn][maxn]; int BFS() { node p, q; p.a=S, p.b=0, p.c=0, p.step=0; memset(vis, 0, sizeof(vis)); vis[S][0][0] = 1; queue<node>Q; Q.push(p); while(Q.size()) { p = Q.front(), Q.pop(); if((p.a==S/2&&p.b==S/2) || (p.b==S/2&&p.c==S/2) || (p.a==S/2&&p.c==S/2)) return p.step; for(int i=0; i<6; i++) { if(i==0) /// a->b { if(p.a+p.b>N) { q.a = p.a+p.b-N; q.b = N; } else { q.a = 0; q.b = p.a+p.b; } q.c = p.c; } else if(i==1) /// a->c { if(p.a+p.b>M) { q.a = p.a+p.c-M; q.c = M; } else { q.a = 0; q.c = p.a+p.c; } q.b = p.b; } else if(i==2) /// b->a { if(p.b+p.a>S) { q.b = p.b+p.a-S; q.a = S; } else { q.b = 0; q.a = p.b+p.a; } q.c = p.c; } else if(i==3) /// b->c { if(p.b+p.c>M) { q.b = p.b+p.c-M; q.c = M; } else { q.b = 0; q.c = p.b+p.c; } q.a = p.a; } else if(i==4) /// c->a { if(p.c+p.a>S) { q.c = p.c+p.a-S; q.a = S; } else { q.c = 0; q.a = p.c+p.a; } q.b = p.b; } else if(i==5) /// c->b { if(p.c+p.b>N) { q.c = p.c+p.b-N; q.b = N; } else { q.c = 0; q.b = p.c+p.b; } q.a = p.a; } q.step = p.step+1; if(!vis[q.a][q.b][q.c]) { vis[q.a][q.b][q.c]=1; Q.push(q); } } } return -1; } int main() { while(scanf("%d%d%d", &S, &N, &M), S+N+M) { if(S%2) { printf("NO\n"); continue; } int ans = BFS(); if(ans==-1) printf("NO\n"); else printf("%d\n", ans); } return 0; }
原文:http://www.cnblogs.com/YY56/p/4796651.html