Description
Input
Output
Sample Input
7 4 3 4 1 3 0 0 0
Sample Output
NO 3
保存每次能互相倒得状态,然后BFS,还算相对巧妙,之后多用用这么写。。。
代码有点长,可以适当封装下,不过这样也挺清晰的。。。
AC代码:
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<ctime> #define eps 1e-6 #define MAX 10005 #define INF 0x3f3f3f3f #define LL long long #define pii pair<int,int> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z) ///map<int,int>mmap; ///map<int,int >::iterator it; using namespace std; int vist[105][105][105],a,b,c;//a b c->S 可乐的体积 , N 和 M是两个杯子的容量 struct node { int a,b,c;//分别表示可乐瓶子,杯子1,杯子2它们里面的可乐体积 int step;//步数 }; int sum=0; void bfs() { queue<node>que; memset(vist,0,sizeof(vist)); node p1; p1.a=a , p1.b=0 , p1.c=0 , p1.step=0; que.push(p1); vist[p1.a][0][0]=1; while(!que.empty()) { p1=que.front(); que.pop(); if((p1.a==a/2&&p1.b==a/2)||(p1.a==a/2&&p1.c==a/2)||(p1.b==a/2&&p1.c==a/2)) { printf("%d\n",p1.step); return; } // node p2; if(p1.a!=0) {//a倒入b if(p1.a>b-p1.b) { p2.a=p1.a-(b-p1.b); p2.b=b; p2.c=p1.c; p2.step=p1.step+1; } else { p2.a=0; p2.b=p1.b+p1.a; p2.c=p1.c; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; que.push(p2); } } if(p1.a!=0) {//a倒入c if(p1.a>c-p1.c) { p2.a=p1.a-(c-p1.c); p2.b=p1.b; p2.c=c; p2.step=p1.step+1; } else { p2.a=0; p2.b=p1.b; p2.c=p1.c+p1.a; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; que.push(p2); } } if(p1.b!=0) {//b倒入a if(p1.b>a-p1.a) { p2.b=p1.b-(a-p1.a); p2.a=a; p2.c=p1.c; p2.step=p1.step+1; } else { p2.b=0; p2.a=p1.a+p1.b; p2.c=p1.c; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; que.push(p2); } } if(p1.b!=0) {//b倒入c if(p1.b>c-p1.c) { p2.b=p1.b-(c-p1.c); p2.a=p1.a; p2.c=c; p2.step=p1.step+1; } else { p2.b=0; p2.a=p1.a; p2.c=p1.c+p1.b; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; que.push(p2); } } if(p1.c!=0) {//c倒入a if(p1.c>a-p1.a) { p2.c=p1.c-(a-p1.a); p2.a=a; p2.b=p1.b; p2.step=p1.step+1; } else { p2.c=0; p2.a=p1.a+p1.c; p2.b=p1.b; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; que.push(p2); } } if(p1.c!=0) {//c倒入b if(p1.c>b-p1.b) { p2.c=p1.c-(b-p1.b); p2.a=p1.a; p2.b=b; p2.step=p1.step+1; } else { p2.c=0; p2.a=p1.a; p2.b=p1.b+p1.c; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; que.push(p2); } } } printf("NO\n"); } int main() { while(~scanf("%d%d%d",&a,&b,&c)) { if(a==0&&b==0&&c==0) break; if(a%2==1) { printf("NO\n"); continue; } bfs(); } return 0; }