[kuangbin带你飞]专题一 简单搜索 [Cloned][NEUQ]

来源:互联网 发布:centos开启22端口 编辑:程序博客网 时间:2024/05/18 13:46

M - 非常可乐

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出”NO”。

Input

三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以”0 0 0”结束。

Output

如果能平分的话请输出最少要倒的次数,否则输出”NO”。

Sample Input

7 4 3
4 1 3
0 0 0

Sample Output

NO
3

思路:

设置三元组(a,b,c)为一个状态量,三个值分别为三个杯子内的可乐的量。那么一开始就是状态(S,0,0)。然后对于三个杯子,只要有杯子是不空的,就可以向另外两个杯子倒可乐。当然也要用一个三维数组记录状态,如果访问过就不再继续。

#include<bits/stdc++.h>using namespace std;int val[3];bool mark[102][102][102];struct state{    int cola[3];    int t;    state(int a,int b,int c,int times)    {        cola[0]=a;        cola[1]=b;        cola[2]=c;        t=times;    }    void print()    {        cout<<cola[0]<<cola[1]<<cola[2]<<endl;    }};int yes( state &ans){    int rec[3];    for(int i=0;i<3;i++)    {        rec[i]=ans.cola[i];    }    sort(rec,rec+3);    if(rec[2]==rec[1]&&!rec[0]) return 1;    if(mark[ans.cola[0]][ ans.cola[1] ][ ans.cola[2] ]==1) return 2;    return 0;}int main(){    int a,b,c;    while(cin>>a>>b>>c&&a&&b&&c)    {        memset(mark,0,sizeof(mark));        int sum=-1;        bool chc=0;        val[0]=a;        val[1]=b;        val[2]=c;        state now(a,0,0,0);        queue <state> q;        q.push(now);        //now.print();        mark[a][ 0 ][ 0]=1;        while(!q.empty()&&!chc)        {            state tem=q.front();            if(yes(tem)==1)            {                chc=1;                sum=tem.t;                continue;            }            for(int i=0;i<3;i++)            {                if(tem.cola[i])                {                    for(int j=0;j<3;j++)                    {                        if(j!=i)                        {                            state neo(0,0,0,tem.t+1);                            neo.cola[j]=min(val[j]-tem.cola[j],tem.cola[i])+tem.cola[j];                            neo.cola[i]=tem.cola[i]-min(val[j]-tem.cola[j],tem.cola[i]);                            neo.cola[3-i-j]=tem.cola[3-i-j];                           // neo.print();                             if(yes(neo)==2)                            {                                //chc=1;                                //neo.print();                                continue;                            }                            //neo.print();                            q.push(neo);                            mark[neo.cola[0]][ neo.cola[1] ][ neo.cola[2] ]=1;                        }                    }                }            }            q.pop();        }        if(sum>=0) cout<<sum<<endl;        else cout<<"NO\n";    }}
阅读全文
0 0
原创粉丝点击