hdu1495 very Coke (BFS)

Source: Internet
Author: User

problem DescriptionIt 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".  Inputthree integers: the volume of S Cola, N and M is the capacity of two cups, ending with "0 0 0".  OutputIf 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 Topic Analysis: BFS violence search, each time from the current state to infer six states, each time out of the team to determine whether the state meets the requirements, there are any two cups in the same water. AC Code:
/** * @xiaoran * breadth-first search, traversing the six states that each state can reach, * if there are two of the same return steps at the end of the water, otherwise traversing all States, no results output no */#include <iostream> #include <cstdio> #include <map> #include <cstring> #include <string> #include <algorithm># include<queue> #include <vector> #include <stack> #include <cstdlib> #include <cctype># Include<cmath> #define LL Long longusing namespace std;struct node{int s,m,n;//Record each of the water int step;//record Steps};int V is[105][105][105];//record is accessed int res,n,m,s;queue<node> q;int Judge (Node A) {//Determine if there are two quilts in the same water if (a.s==a.m& &a.n==0| | a.s==a.n&&a.m==0| |    a.m==a.n&&a.s==0) return 1; return 0;}    int BFS () {//wide search res=-1;    Node A, B;    A.s=s;    A.m=a.n=a.step=0;    Q.push (a);        vis[s][0][0]=1;//Access start state while (Q.size ()) {A=q.front ();        Q.pop ();            if (Judge (a)) {//Determines whether the condition Res=a.step is satisfied;            cout<<a.step<<endl;        Break        }//traversal of six state int tmp; if (a.s!=0) {//s to M or n pour water if (a.n!=n) {//Current n dissatisfied tmp=min (A.S,N-A.N);//find can pour de water b=a;                b.s-=tmp;                b.n+=tmp;                b.step=a.step+1; if (vis[b.s][b.n][b.m]==0) {//The current state does not have access to Q.push (b);//Put the queue vis[b.s][b.n][b.m]=1;//tag to access, which Can be understood as the gray state defined in the introduction to the algorithm} if (a.m!=m) {//current m is dissatisfied with tmp=min (A.S,M-A.M);//find can pour de water                Volume b=a;                b.s-=tmp;                b.m+=tmp;                b.step=a.step+1; if (vis[b.s][b.n][b.m]==0) {//The current state does not have access to Q.push (b);//Put the queue vis[b.s][b.n][b.m]=1;//tag to access, which                Can be understood as the gray state defined in the introduction of the Algorithm}}} if (a.n!=0) {//n to S or M in water pour if (a.s!=s) {//current s dissatisfied                Tmp=min (A.N,S-A.S);//find can pour de water b=a;                b.n-=tmp;                b.s+=tmp;                b.step=a.step+1; if (vis[b.s][b.n][b.m]==0) {//Current status notHave access Q.push (b);//Put the queue vis[b.s][b.n][b.m]=1;//tag to access, which can be understood as the gray state defined in the introduction to the algorithm                }} if (a.m!=m) {//current M dissatisfied tmp=min (A.N,M-A.M);//find can pour de water b=a;                b.n-=tmp;                b.m+=tmp;                b.step=a.step+1; if (vis[b.s][b.n][b.m]==0) {//The current state does not have access to Q.push (b);//Put the queue vis[b.s][b.n][b.m]=1;//tag to access, which                Can be understood as the gray state defined in the introduction of the Algorithm}}} if (a.m!=0) {//n to S or M in water pour if (a.s!=s) {//current s dissatisfied                Tmp=min (A.M,S-A.S);//find can pour de water b=a;                b.m-=tmp;                b.s+=tmp;                b.step=a.step+1; if (vis[b.s][b.n][b.m]==0) {//The current state does not have access to Q.push (b);//Put the queue vis[b.s][b.n][b.m]=1;//tag to access, which Can be understood as the gray state defined in the introduction of the algorithm} if (a.n!=n) {//Current n tmp=min (A.M,N-A.N);//Find a way to pour de water   Volume b=a;             b.m-=tmp;                b.n+=tmp;                b.step=a.step+1; if (vis[b.s][b.n][b.m]==0) {//The current state does not have access to Q.push (b);//Put the queue vis[b.s][b.n][b.m]=1;//tag to access, which Can be understood as the gray state defined in the introduction to the Algorithm}}}} return res;        int main () {while (cin>>s>>n>>m,s+n+m) {memset (vis,0,sizeof (VIS));        while (!q.empty ()) {//empties Duil Q.pop ();            } if (s%2==1) {cout<< "NO" <<endl;        Continue            } if (n==m) {cout<< "1" <<endl;        Continue        } BFS ();        if (res==-1) cout<< "NO" <<endl;    else cout<<res<<endl; } return 0;}


hdu1495 very Coke (BFS)

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.