标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8021 Accepted Submission(s):
3210
#include<stdio.h> #include<string.h> #include<queue> #include<stack> #include<algorithm> #define INF 0x3f3f3f #define MAX 100+10 using namespace std; int s,n,m,k; int map[MAX][MAX][MAX]; struct node { int a,b,c,step; friend bool operator <(node x,node y) { return x.step>y.step; } }; void bfs() { node now,next;//用next表示上一次,now表示当前 priority_queue<node> q; now.a=s; now.b=0; now.c=0; now.step=0; q.push(now); map[now.a][now.b][now.c]=1; while(!q.empty()) { next=q.top(); q.pop(); if(next.a==k&&next.b==k||next.a==k&&next.c==k||next.b==k&&next.c==k) { printf("%d\n",next.step); return ; } /*a杯子倒进b,c两个里面*/ /*先倒进b杯子里*/ if(next.b!=n&&next.a)//前提b杯子不能满 { if(next.a>(n-next.b))// 把a杯子的水倒满b a还有剩余 { now.a=next.a-(n-next.b); now.b=n; now.c=next.c;//c杯子不变 if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1;//记录状态 q.push(now); } } else//a杯子水倒不满b { now.a=0; now.b=next.a+next.b; now.c=next.c; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } } /*倒进c杯子里面*/ if(next.c!=m&&next.a)//前提c杯子不能满 a杯子水不能为空 { if(next.a>(m-next.c))//倒满还有剩余 { now.a=next.a-(m-next.c); now.b=next.b;//b杯子不变 now.c=m; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } else//倒不满 { now.a=0; now.b=next.b; now.c=next.c+next.a; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } } /*b杯子水倒进a,c里面*/ /*先倒进a*/ if(next.b&&next.a!=s)//b杯子水不能为空 且a杯子水不能满 { if(next.b>(s-next.a))//倒满还有剩余 { now.a=s; now.b=next.b-(s-next.a); now.c=next.c; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } else//倒不满 { now.a=next.b+next.a; now.b=0; now.c=next.c; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } } /*倒进c*/ if(next.b&&next.c!=m)//b杯子水不能为空 且c杯子水不能满 { if(next.b>(m-next.c))//倒满还有剩余 { now.a=next.a; now.b=next.b-(m-next.c); now.c=m; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } else//倒不满 { now.a=next.a; now.b=0; now.c=next.c+next.b; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } } /*c杯子倒进a,b里面*/ /*先 a*/ if(next.c&&next.a!=s) { if(next.c>(s-next.a))//倒满还有剩余 { now.a=s; now.b=next.b; now.c=next.c-(s-next.a); if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } else//倒不满a { now.a=next.a+next.c; now.b=next.b; now.c=0; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } } /*倒进b*/ if(next.c&&next.b!=n) { if(next.c>(n-next.b))//倒满还有剩余 { now.a=next.a; now.b=n; now.c=next.c-(n-next.b); if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } else//倒不满b { now.a=next.a; now.b=next.b+next.c; now.c=0; if(!map[now.a][now.b][now.c])//没有出现该状态 { now.step=next.step+1; map[now.a][now.b][now.c]=1; q.push(now); } } } } printf("NO\n"); } int main() { int t,i,j,sum; while(scanf("%d%d%d",&s,&n,&m)&&(s!=0||n!=0||m!=0)) { if(s&1) { printf("NO\n"); continue; } memset(map,0,sizeof(map)); k=s/2; bfs(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4940403.html