C语言编程练习60:非常可乐

题目描述

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

输入

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

输出

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

样例输入

7 4 2
6 3 3
6 4 2
6 1 1
6 4 1
0 0 0

样例输出

NO
1
NO
NO
3


思路:搜索虽然做好几道但是没有摸着门路,难受,直接看的大佬的
    #include<bits/stdc++.h>
    using namespace std;
    bool flag[103][103][103];//表示三个容器中现在分别有多少饮料;
    int n,m,s;
    struct cool
    {
        int x,y,z,step;
    }pre,nextt;  //刚开始用next,GC++编译不过去;
    void change(int i,int j)  //该函数分别讨论倒水的六种情况;
    {
        if(i==1&&j==2)
        {
           nextt.x=pre.x-(m-pre.y);
           if(nextt.x<0)nextt.x=0;  //再分类讨论;
           nextt.y=pre.y+pre.x-nextt.x;
     
        }
        else if(i==1&&j==3)  //上同;
        {
            nextt.x=pre.x-(s-pre.z);
            if(nextt.x<0)nextt.x=0;
            nextt.z=pre.z+pre.x-nextt.x;
        }
        else if(i==2&&j==1)//上同;
        {
            nextt.y=pre.y-(n-pre.x);
            if(nextt.y<0)nextt.y=0;
            nextt.x=pre.x+pre.y-nextt.y;
     
        }
        else if(i==2&&j==3)//上同;
        {
            nextt.y=pre.y-(s-pre.z);
            if(nextt.y<0)nextt.y=0;
            nextt.z=pre.z+pre.y-nextt.y;
        }
        else if(i==3&&j==1)//上同;
        {
            nextt.z=pre.z-(n-pre.x);
            if(nextt.z<0)nextt.z=0;
            nextt.x=pre.x+pre.z-nextt.z;
        }
        else if(i==3&&j==2)//上同;
        {
            nextt.z=pre.z-(m-pre.y);
            if(nextt.z<0)nextt.z=0;
            nextt.y=pre.y+pre.z-nextt.z;
     
        }
     
     
    }
    int bfs(int a,int b,int c)
    {
        queue < cool > Q;  //老问题,“敲黑板”,bfs函数内定义队列;
        pre.x=a;pre.y=b;pre.z=c;
        pre.step=0;
        flag[a][b][c]=1;
        Q.push(pre);
        while(!Q.empty())
        {
            pre=Q.front();
            Q.pop();
            for(int i=1;i<=3;++i)  //用一个2层循环枚举6种情况;
                for(int j=1;j<=3;++j)
                {
                    nextt=pre;
                    if(i!=j)
                    {
                        change(i,j);  //“先得到下一个点的坐标再判断”;
                        if(flag[nextt.x][nextt.y][nextt.z]==0)
                        {
                            flag[nextt.x][nextt.y][nextt.z]=1;//符合条件的点(基本思路):标记、计算step、入队;
                            nextt.step=pre.step+1;
                            if((nextt.y==nextt.z&&nextt.y==n/2)||(nextt.y==nextt.x&&nextt.x==n/2)||(nextt.x==nextt.z&&nextt.x==n/2))
                                return nextt.step;
                            else
                                Q.push(nextt);
                        }
     
                    }
                }
        }
        return -1;
    }
    int main()
    {
        while(cin>>n>>m>>s&&n+s+m!=0)
        {
            if((n&1)==1)  //奇数一定无法均分,因为杯子的刻度都是整数;
            {
                cout<<"NO"<<endl;
            }
            else
            {
                memset(flag,0,sizeof(flag));//多组测试数据,“敲黑板”;
                int ans;
                ans=bfs(n,0,0);
                if(ans>=0)cout<<ans<<endl;
                else cout<<"NO"<<endl;
            }
        }
        return 0;
    }

 参考:https://blog.csdn.net/qq_41661919/article/details/79595063

       https://blog.csdn.net/weixin_42250655/article/details/81433498

posted @ 2021-02-24 19:35  FantasticDoulbeFish  阅读(211)  评论(0编辑  收藏  举报