HDU 1495 very Coke (BFS)

Source: Internet
Author: User

Very coke.Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5370 Accepted Submission (s): 2191


Problem description Everyone must feel the exercise after drinking Coke is a very pleasant thing, but seeyou don'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 it can be divided into a minimum number of times to pour, otherwise output "NO".
Sample Input
7 4 34 1 30 0 0

Sample Output
NO3

Authorseeyou
Source "2006 Campus cultural activity Month" of "School Anniversary Cup" College students Program Design Competition and Hangzhou University of Engineering fourth session of college students Program design contest
Recommendll   |   we has carefully selected several similar problems for you:   1175 1253 1072 1372 1180  : 6 Kinds of situations: s->n,s->m,n->s,n->m,m->s , m->n. Each type of payment is also divided into full and pour dissatisfaction.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 100010#define Mod 10000007#define Lson l,mid,idx<<1#  Define Rson mid+1,r,idx<<1|1#define LC idx<<1#define RC Idx<<1|1const Double EPS = 1e-11;const double PI = ACOs ( -1.0); const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace Std;int s,n,m;b    ool vis[111][111][111],flag;struct node{int s,n,m; int num;};    Queue<node>que;void BFS () {while (Que.size ()) Que.pop ();    memset (vis,0,sizeof Vis);    Node a,t;    a.s=s,a.n=0,a.m=0,a.num=0;    Que.push (a);    Vis[s][0][0]=1;        while (Que.size ()) {A=que.front ();        Que.pop (); if (a.s==a.n&&a.m==0| | (A.S==0&AMP;&AMP;A.N==A.M) | |        (A.N==0&AMP;&AMP;A.S==A.M)) {Flag=0;            cout<<a.num<<endl;        Return                 } if (a.s>0)//s->n,s->m {if (a.n<n)//s->n {int TT=N-A.N;                    if (A.S&GT;TT)//n can be filled with {t.s=a.s-tt;                    T.n=n;                T.M=A.M;                    } else//cannot be filled with {t.s=0;                    T.N=A.S+A.N;                T.M=A.M;                    } if (!vis[t.s][t.n][t.m]) {t.num=a.num+1;                    Que.push (t);                Vis[t.s][t.n][t.m]=1;                }} if (a.m<m) {int tt=m-a.m;                    if (A.S&GT;TT) {t.s=a.s-tt;                    T.N=A.N;                T.m=m;                    } else {t.s=0;   T.N=A.N;                 T.M=A.M+A.S;                    } if (!vis[t.s][t.n][t.m]) {t.num=a.num+1;                    Que.push (t);                Vis[t.s][t.n][t.m]=1;                }}} if (a.n>0) {if (a.s<s) {int tt=s-a.s;                    if (A.N&GT;TT) {t.n=a.n-tt;                    T.M=A.M;                T.s=s;                    } else {t.n=0;                    T.M=A.M;                T.S=A.S+A.N;                    } if (!vis[t.s][t.n][t.m]) {t.num=a.num+1;                    Que.push (t);                Vis[t.s][t.n][t.m]=1;                }} if (a.m<m) {int tt=m-a.m;                    if (A.N&GT;TT) {t.n=a.n-tt;       T.m=m;             T.S=A.S;                    } else {t.n=0;                    T.M=A.M+A.N;                T.S=A.S;                    } if (!vis[t.s][t.n][t.m]) {t.num=a.num+1;                    Que.push (t);                Vis[t.s][t.n][t.m]=1;                }}} if (a.m>0) {if (a.s<s) {int tt=s-a.s;                    if (A.M&GT;TT) {T.N=A.N;                    T.m=a.m-tt;                T.s=s;                    } else {T.N=A.N;                    t.m=0;                T.S=A.S+A.M;                    } if (!vis[t.s][t.n][t.m]) {t.num=a.num+1;                    Que.push (t);                Vis[t.s][t.n][t.m]=1;       }} if (A.n<n) {         int TT=N-A.N;                    if (A.M&GT;TT) {t.n=n;                    T.m=a.m-tt;                T.S=A.S;                    } else {t.n=a.n+a.m;                    t.m=0;                T.S=A.S;                    } if (!vis[t.s][t.n][t.m]) {t.num=a.num+1;                    Que.push (t);                Vis[t.s][t.n][t.m]=1; }}}}}int main () {while (cin>>s>>n>>m&& (s+n+m)) {if (s%2)//Odd            Cannot divide {cout<< "no\n";        Continue        } flag=1;        BFS ();    if (flag) cout<< "NO" <<endl; } return 0;}


HDU 1495 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.