标签:
#include"cstdio" #include"cstring" #include"queue" #include"algorithm" using namespace std; const int MAXN=105; struct node{ int s,n,m,step; node(int cs,int cn,int cm,int cstep):s(cs),n(cn),m(cm),step(cstep){} node(){} }; int S,N,M; int vis[MAXN][MAXN][MAXN]; bool judge(int s,int n,int m) { if((s==n&&m==0)||(s==m&&n==0)||(n==m&&s==0)) return true; return false; } void bfs() { memset(vis,0,sizeof(vis)); queue<node> que; que.push(node(S,0,0,0)); vis[S][0][0]=1; while(!que.empty()) { node now = que.front();que.pop(); if(judge(now.s,now.n,now.m)) { printf("%d\n",now.step); return; } int ts,tn,tm; //S->N; ts=now.s-min(N-now.n,now.s);//不带刻度倒水经典公式 tn=now.n+min(N-now.n,now.s); tm=now.m; if(!vis[ts][tn][tm]) { vis[ts][tn][tm]=1; que.push(node(ts,tn,tm,now.step+1)); } //S->M; ts=now.s-min(M-now.m,now.s); tm=now.m+min(M-now.m,now.s); tn=now.n; if(!vis[ts][tn][tm]) { vis[ts][tn][tm]=1; que.push(node(ts,tn,tm,now.step+1)); } //N->S; S>=N ts=now.s+now.n; tn=0; tm=now.m; if(!vis[ts][tn][tm]) { vis[ts][tn][tm]=1; que.push(node(ts,tn,tm,now.step+1)); } //N->M ts=now.s; tn=now.n-min(M-now.m,now.n); tm=now.m+min(M-now.m,now.n); if(!vis[ts][tn][tm]) { vis[ts][tn][tm]=1; que.push(node(ts,tn,tm,now.step+1)); } //M->S;S>=M ts=now.s+now.m; tn=now.n; tm=0; if(!vis[ts][tn][tm]) { vis[ts][tn][tm]=1; que.push(node(ts,tn,tm,now.step+1)); } //M->N ts=now.s; tn=now.n+min(N-now.n,now.m); tm=now.m-min(N-now.n,now.m); if(!vis[ts][tn][tm]) { vis[ts][tn][tm]=1; que.push(node(ts,tn,tm,now.step+1)); } } printf("NO\n"); } int main() { while(scanf("%d%d%d",&S,&N,&M)&&S&&N&&M) { if(S%2) { printf("NO\n"); } else bfs(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5016931.html