HDU Search Practice very Coke

Source: Internet
Author: User

Very Coke Time limit:2000/1000ms (java/other) Memory limit:32768/32768k (Java/other) total submission (s): Wuyi Accepted Submis Sion (s): 21Problem 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 not 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 it can be divided into a minimum number of times to pour, otherwise output "NO".

Sample Input7 4 34 1 30 0 0

Sample OutputNO3 Simple test instructions: the title meaning is pour coke, divided into the same points, first judge can be the same, and then ask the least to how many times thinking analysis: Breadth search, have the goal, until the Pour Cola, to achieve the goal so far
#include <iostream>#include<algorithm>#include<queue>#include<cstring>using namespaceStd//according to the coursewareBOOL  is[101][101];intm, n, S, si, SJ;structnode{intX,y,all,t;//X,y,all represents the volume of Coke in the M,n,s Cup, and T indicates how many times it has been poured};voidBFs () {Queue<node>que; memset ( is,false,sizeof( is));    Node p,q; P.x=0, p.y =0, p.t =0, P.all =s;    Que.push (P);  is[P.x] [P.Y] =true;  while(!Que.empty ()) {P=Que.front ();        Que.pop (); if(p.y = = P.all && P.y = = s/2) {cout<< p.t <<Endl; return; }        if(P.all+p.x > m)//s Inverted m{q.x= M,q.y = P.y,q.all = p.all+p.x-m,q.t = p.t+1; if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; }        Else{q.x= P.all+p.x,q.y = P.y,q.all =0, q.t = p.t+1; if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; }        if(P.all+p.y > N)//s pour n{q.x= p.x, Q.y = n, q.all = p.all+p.y-n,q.t = p.t+1; if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; }        Else{q.x= P.x,q.y = P.all+p.y,q.all =0, q.t = p.t+1; if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; }        if(P.x+p.y > N)//m pour n{q.x= P.x+p.y-n,q.y = N,q.all = p.all,q.t = p.t+1; if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; }        Else{q.x=0, q.y = P.x+p.y,q.all = p.all,q.t = p.t+1; if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; } Q.all= p.all+p.x,q.x =0, q.y = p.y,q.t = p.t+1;//m Inverted S        if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; if(P.x+p.y >m) {q.y= p.y+p.x-m,q.x = M,q.all = p.all,q.t = p.t+1;//N Inverted m            if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; }        Else{q.x= P.X+P.Y,Q.Y =0, Q.all = p.all,q.t = p.t+1; if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; } Q.all= p.all+p.y,q.x = P.x,q.y =0, q.t = p.t+1;//N Inverted S        if(! is[q.x][q.y]) Que.push (q), is[Q.x] [Q.Y] =true; } cout<<"NO"<<Endl;}intMain () { while(Cin >> s >> m >>N) {if(s = =0&& m = =0&& n = =0)         Break; if(s%2) {cout<<"NO"<<Endl; Continue; }        if(M >N) swap (m,n);    BFS (); }    return 0;}

  

HDU Search Practice very Coke

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.