Online questions:
Very cola
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4482 accepted submission (s): 1815
Problem description everyone must feel that drinking cola after exercise is a pleasant thing, but seeyou does not think so. Because every time seeyou buy a cola, a ox will ask seeyou to share this bottle of cola, and there must be as much to drink as seeyou. But seeyou has only two cups in his hand. Their capacities are n milliliters and M milliliters respectively. Their capacities are S (S <101) milliliters (just filled with a bottle ), they can pour cola between each other (both have no scale, and S = N + M, 101> S> 0, N> 0, m> 0 ). Smart acmer, do you mean they can be evenly divided? If you can output the least number of times of cola dumping, if you cannot output "no ".
Input three integers: The size of S cola, N and m are the capacity of the two cups, ended with "0 0 0.
If the output value can be evenly divided, the output value must be inverted at least. Otherwise, the output value is "no ".
Sample input7 4 34 1 30 0 0
The meaning of sample outputno3 is not explained in Chinese. You can directly use BFs to record the status of the three current cups during the extensive search. Then, you can repeat the results. Here, we use set to save the extended state. Code:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 #include <queue> 6 #include <utility> 7 #define x first 8 #define y second 9 using namespace std; 10 11 typedef pair<pair<int,int>,int> piii; 12 int s,n,m,ti; 13 14 set<piii> ss; 15 16 bool isok(piii a){ 17 if(a.x.x*2==s && a.x.y*2==s) return 1; 18 else if(a.x.x*2==s && a.y*2==s) return 1; 19 else if(a.x.y*2==s && a.y*2==s) return 1; 20 return 0; 21 } 22 23 inline piii mpiii(int a,int b,int c) { return make_pair( make_pair(a,b) , c ); } 24 25 bool check(){ 26 ti=0; 27 queue<pair<piii,int> > q; 28 pair<piii,int> tt; 29 piii u = mpiii(s,0,0); 30 q.push(make_pair(u,0)); 31 ss.insert(u); 32 while(!q.empty()){ 33 int tti; 34 tt = q.front(); 35 q.pop(); 36 u = tt.x; 37 tti = tt.y; 38 //s-->n 39 if(u.x.x>0 && (n-u.x.y)>0){ 40 if( u.x.x > (n-u.x.y) ){ u.x.x-=(n-u.x.y); u.x.y=n; } 41 else{ u.x.y+=u.x.x; u.x.x=0; } 42 if(ss.count(u)<=0){ 43 tti++; 44 if(isok(u)){ti=tti; return 1;} 45 ss.insert(u); 46 q.push( make_pair(u,tti) ); 47 } 48 } 49 //s-->m 50 u = tt.x; 51 tti = tt.y; 52 if(u.x.x>0 && (m-u.y)>0){ 53 if( u.x.x > (m-u.y) ){ u.x.x-=(m-u.y); u.y=m; } 54 else{ u.y+=u.x.x; u.x.x=0; } 55 if(ss.count(u)<=0){ 56 tti++; 57 if(isok(u)){ti=tti; return 1;} 58 ss.insert(u); 59 q.push( make_pair(u,tti) ); 60 } 61 } 62 //n-->s 63 u = tt.x; 64 tti = tt.y; 65 if(u.x.y>0 && (s-u.x.x)>0){ 66 if( u.x.y > (s-u.x.x) ){ u.x.y-=(s-u.x.x); u.x.x=s; } 67 else{ u.x.x+=u.x.y; u.x.y=0; } 68 if(ss.count(u)<=0){ 69 tti++; 70 if(isok(u)){ti=tti; return 1;} 71 ss.insert(u); 72 q.push( make_pair(u,tti) ); 73 } 74 } 75 //n-->m 76 u = tt.x; 77 tti = tt.y; 78 if(u.x.y>0 && (m-u.y)>0){ 79 if( u.x.y > (m-u.y) ){ u.x.y-=(m-u.y); u.y=m; } 80 else{ u.y+=u.x.y; u.x.y=0;} 81 if(ss.count(u)<=0){ 82 tti++; 83 if(isok(u)){ti=tti; return 1;} 84 ss.insert(u); 85 q.push( make_pair(u,tti) ); 86 } 87 } 88 //m-->s 89 u = tt.x; 90 tti = tt.y; 91 if(u.y>0 && (s-u.x.x)>0){ 92 if( u.y > (s-u.x.x) ){ u.y-=(s-u.x.x); u.x.x=s; } 93 else{ u.x.x+=u.y; u.y=0; } 94 if(ss.count(u)<=0){ 95 tti++; 96 if(isok(u)){ti=tti; return 1;} 97 ss.insert(u); 98 q.push( make_pair(u,tti) ); 99 }100 }101 //m-->n102 u = tt.x;103 tti = tt.y;104 if(u.y>0 && (n-u.x.y)>0){105 if( u.y > (n-u.x.y) ){ u.y-=(n-u.x.y); u.x.y=n; }106 else{ u.x.y+=u.y; u.y=0; }107 if(ss.count(u)<=0){108 tti++;109 if(isok(u)){ti=tti; return 1;}110 ss.insert(u);111 q.push( make_pair(u,tti) );112 }113 }114 }115 return 0;116 }117 118 int main()119 {120 //freopen("data.txt","r",stdin);121 while(scanf("%d %d %d",&s,&n,&m),(s+n+m)){122 ss.clear();123 if( (s&1)==0 && check()) printf("%d\n",ti);124 else printf("NO\n");125 }126 return 0;127 }1495