标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11034 Accepted Submission(s): 4458
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <stdlib.h> using namespace std; struct Node{ int x,y,z,step; }; int a,b,c; bool vis[105][105][105]; int bfs(){ memset(vis,false,sizeof(vis)); queue<Node> q; Node s; s.x = a,s.y = 0,s.z = 0,s.step = 0; vis[a][0][0]=true; q.push(s); while(!q.empty()){ Node now = q.front(); q.pop(); if(now.x==a/2&&now.y==a/2||now.x==a/2&&now.z==a/2||now.y==a/2&&now.z==a/2){ return now.step; } Node next; if(now.x!=0){ /// a -> b if(now.x>b-now.y){ ///还可以倒满b next.x =now.x-(b-now.y); next.y = b; next.z = now.z; }else{ next.x = 0; next.y =now.x+now.y; next.z = now.z; } next.step=now.step+1; if(!vis[next.x][next.y][next.z]){ vis[next.x][next.y][next.z] = true; q.push(next); } } if(now.x!=0){ ///a->c if(now.x>c-now.z){ next.x = now.x - (c-now.z); next.y = now.y; next.z = c; }else{ next.x = 0; next.y = now.y; next.z =now.z+now.x; } next.step=now.step+1; if(!vis[next.x][next.y][next.z]){ vis[next.x][next.y][next.z] = true; q.push(next); } } if(now.y!=0){ ///b->a if(now.y>a-now.x){ next.x = a; next.y = now.y - (a-now.x); next.z = now.z; }else{ next.x = now.x+now.y; next.y = 0; next.z =now.z; } next.step=now.step+1; if(!vis[next.x][next.y][next.z]){ vis[next.x][next.y][next.z] = true; q.push(next); } } if(now.y!=0){ ///b->c if(now.y>c-now.z){ next.x = now.x; next.y = now.y - (c-now.z); next.z = c; }else{ next.x = now.x; next.y = 0; next.z =now.z+now.y; } next.step=now.step+1; if(!vis[next.x][next.y][next.z]){ vis[next.x][next.y][next.z] = true; q.push(next); } } if(now.z!=0){ ///c->a if(now.z>a-now.x){ next.x = a; next.y = now.y; next.z = now.z - (a-now.x); }else{ next.x = now.x+now.z; next.y = now.y; next.z = 0; } next.step=now.step+1; if(!vis[next.x][next.y][next.z]){ vis[next.x][next.y][next.z] = true; q.push(next); } } if(now.z!=0){ ///c->b if(now.z>b-now.y){ next.x = now.x; next.y = b; next.z = now.z - (b-now.y); }else{ next.x = now.x; next.y = now.y+now.z; next.z = 0; } next.step=now.step+1; if(!vis[next.x][next.y][next.z]){ vis[next.x][next.y][next.z] = true; q.push(next); } } } return -1; } int main(){ while(scanf("%d%d%d",&a,&b,&c)!=EOF,a+b+c){ if(a%2==1){ printf("NO\n"); continue; } int ans = bfs(); if(ans==-1){ printf("NO\n"); }else{ printf("%d\n",ans); } } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5738357.html