You must feel that drinking coke after exercise is a very comfortable thing, but see you don't think so. Because every time seeyou bought a coke, a Niu asked to share this coke with seeyou, and he must drink as much as seeyou. However, there are only two cups in seeyou's hand. Their capacity is N ml and M ml respectively. The volume of coke is s (s < 101) ml (just full of a bottle). The three cups can pour coke to each other (all without scale, and S==N+M, 101 > s > 0, N > 0, M > 0). Smart ACMER do you think they can share equally? If you can output the minimum number of times to pour coke, if you can't output "NO".
Input
Three integers: the volume of S cola, N and M are the capacity of two cups, ending with "0.00".
Output
If it can be divided equally, please output the minimum number of times to be inverted, otherwise output "NO".
Sample Input
7 4 3 4 1 3 0 0 0
Sample Output
NO 3
Code
#include "iostream" #include "cstring" #include "queue" using namespace std; struct point { int n,m,s; int step; point(int a,int b,int c) { s=a; n=b; m=c; } point() { } }; int vis[102][102][102]; int n,m,s; void bfs() { point start(s,0,0); vis[s][0][0]=1; start.step=0; int end=s/2; if(s%2==1) { cout<<"NO"<<endl; return; } queue<point> que; que.push(start); while(!que.empty()) { point cur=que.front(); que.pop(); if(cur.m==end&&cur.n==end||cur.n==end&&cur.s==end||cur.m==end&&cur.s==end) { cout<<cur.step<<endl; return ; } //It's especially strange that when you use return, you can't get the answer for(int i=1;i<=6;i++) { point tmp=cur; switch(i) { case 1: if(tmp.s>=(n-tmp.n)) { tmp.s-=(n-tmp.n); tmp.n=n; } else { tmp.n+=tmp.s; tmp.s=0; } break; case 2: if(tmp.s>=(m-tmp.m)) { tmp.s-=(m-tmp.m); tmp.m=m; } else { tmp.m+=tmp.s; tmp.s=0; } break; case 3: if(tmp.m>=(n-tmp.n)) { tmp.m-=(n-tmp.n); tmp.n=n; } else { tmp.n+=tmp.m; tmp.m=0; } break; case 4: if(tmp.m>=(s-tmp.s)) { tmp.m-=(s-tmp.s); tmp.s=s; } else { tmp.s+=tmp.m; tmp.m=0; } break; case 5: if(tmp.n>=(s-tmp.s)) { tmp.n-=(s-tmp.s); tmp.s=s; } else { tmp.s+=tmp.n; tmp.n=0; } break; case 6: if(tmp.n>=(m-tmp.m)) { tmp.n-=(m-tmp.m); tmp.m=m; } else { tmp.m+=tmp.n; tmp.n=0; } break; } tmp.step++; if(tmp.m>=0&&tmp.n>=0&&tmp.s>=0&&tmp.n<=n&&tmp.m<=m&&tmp.s<=s&&vis[tmp.s][tmp.n][tmp.m]==0) { vis[tmp.s][tmp.n][tmp.m]=1; que.push(tmp); } } } cout<<"NO"<<endl; return; } int main() { while(scanf("%d%d%d",&s,&n,&m)&&n!=0&&m!=0&&s!=0) { memset(vis,0,sizeof(vis)); bfs(); } return 0; }