very Coke .Time
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U
Description
It is a pleasant thing to drink coke after exercise, but Seeyou doesn't think so. Because every time when Seeyou bought Coke, Ox asked to share this bottle of Coke with seeyou, and must drink as much as seeyou. But Seeyou's hands only two cups, their capacity is N ml and M ml cola volume for S (s<101) ml (just fill a bottle), they can pour coke between three each other (there is no scale, and s==n+m,101>s>0,n>0,m>0). Smart Acmer, do you think they can split it? If you can output the minimum number of Coke, if you cannot output "no".
Input
Three integers: The volume of S Cola, N and M is the capacity of two cups, ending with "0 0 0".
Output
If you can divide it, please output a minimum number of times, otherwise output "NO".
Sample Input
7 4 34 1 30 0 0
Sample Output
NO3
Compare the underlying BFS
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < queue> #include <vector> #include <iomanip> #include <math.h> #include <map>using namespace STD; #define FIN freopen ("Input.txt", "R", stdin), #define FOUT freopen ("Output.txt", "w", stdout); #define INF 0x3f3f 3f3f#define infll 0x3f3f3f3f3f3f3f#define Lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef Long Long LL ; struct node{int ml[3]; int times;}; int Cola[3];int vis[105][105][105];d ouble ave;queue<node>q;int bfs (int x,int y,int z) {while (!q.empty ()) {Q.pop ( );} Node fir; Fir.ml[0]=x; Fir.ml[1]=y; Fir.ml[2]=z; fir.times=0; Q.push (FIR); while (!q.empty ()) {node P=q.front (); Q.pop (); for (int i=0;i<3;i++) for (int j=0;j<3;j++) {if (i==j) continue; Node P1; if (P.ml[i]+p.ml[j]<cola[j]) {p1.ml[i]=0; P1.ML[J]=P.ML[I]+P.ML[J]; }else{P1.ml[i]=p.ml[i]+p.ml[j]-cola[j]; P1.ML[J]=COLA[J]; } for (int ii=0;ii<3;ii++) {if (ii==i| | II==J) continue; P1.ML[II]=P.ML[II]; } if (Vis[p1.ml[0]][p1.ml[1]][p1.ml[2]]) continue; Vis[p1.ml[0]][p1.ml[1]][p1.ml[2]]=1; p1.times=p.times+1; Q.push (p1); if ((p1.ml[0]==ave&&p1.ml[0]==p1.ml[1)) | | (p1.ml[0]==ave&&p1.ml[0]==p1.ml[2]) | | (P1.ml[1]==ave&&p1.ml[1]==p1.ml[2])) {printf ("%d\n", p1.times); return 0; }}} printf ("no\n");} int main () {//fin int s,n,m; while (~SCANF ("%d%d%d", &s,&n,&m)) {if (s==0&&n==0&&m==0) break; memset (vis,0,sizeof (VIS)); Cola[0]=s; Cola[1]=n; COLA[2]=m; ave=s/2.0; Here must/2.0 BFS (s,0,0); }}
HDU 1495 very Coke BFS