• 非常可乐(杭电hdu1495)bfs


     

    非常可乐

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3510    Accepted Submission(s): 1440
    Problem Description
    大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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

     这是一题搜索题,bfs,我的思路是分为A->B , A->C , B->A , B->C , C->B , C->A 六种情况,后面4种情况,要考虑是否溢出,,,仔细一点就不会错了,哈哈

    附上我的代码,有什么更好的方法,请多多指教,                                                            ps:原题 http://acm.hdu.edu.cn/showproblem.php?pid=1495

    #include<iostream>
    #include<queue>
    #include<cstring>
    using namespace std;
    int visit[105][105][105];
    int s,m,n;
    
    struct node
    {
        int x,y,z;
        int step;
    };
    
    void bfs()
    {
        queue<node>Q;    
        node now,next;
        now.x=s;
        now.y=0;
        now.z=0;
        visit[now.x][now.y][now.z]=1;
        now.step=0;
        Q.push(now);
        while(!Q.empty())
        {
            now=Q.front();
            Q.pop();
            if((now.x==s/2&&now.x==now.y)||(now.x==s/2&&now.x==now.z)||(now.y==s/2&&now.y==now.z))//到达评分条件的时候跳出
            {
                printf("%d
    ",now.step);
                return ;
            }
            if(now.x != 0) //A->B,A->C
            {
                if(now.y != n)
                {
                    next.x = now.x - (n - now.y);
                    next.y = n;
                    next.z = now.z;
        
                    if(!visit[next.x][next.y][next.z])
                    {
                        next.step = now.step + 1;
                        Q.push(next);
                        visit[next.x][next.y][next.z] = 1;
                    }
                }
                if(now.z != m)
                {
                    next.x = now.x - (m - now.z);
                    next.y = now.y;
                    next.z = m;
    
                    if(!visit[next.x][next.y][next.z])
                    {
                        next.step = now.step + 1;
                        Q.push(next);
                        visit[next.x][next.y][next.z] = 1;
                    }
                }
            }
            if(now.y != 0) //B->A,B->C
            {
                next.x = now.x + now.y;
                next.y = 0;
                next.z = now.z;
      
                if(!visit[next.x][next.y][next.z])
                {
                    next.step = now.step + 1;
                    Q.push(next);
                    visit[next.x][next.y][next.z] = 1;
                }
                if(now.y < m - now.z)
                {
                    next.x = now.x;
                    next.y = 0;
                    next.z = now.y + now.z;
                    if(!visit[next.x][next.y][next.z])
                    {
                        next.step = now.step + 1;
                        Q.push(next);
                        visit[next.x][next.y][next.z] = 1;
                    }
                }
                else
                {
                    next.x = now.x;
                    next.y = now.y - (m - now.z);
                    next.z = m;
                    if(!visit[next.x][next.y][next.z])
                    {
                        next.step = now.step + 1;
                        Q.push(next);
                        visit[next.x][next.y][next.z] = 1;
                    }
                }
            }
            if(now.z != 0) //C->A,C->B
            {
                next.x = now.x + now.z;
                next.y = now.y;
                next.z = 0;
                if(!visit[next.x][next.y][next.z])
                    {
                        next.step = now.step + 1;
                        Q.push(next);
                        visit[next.x][next.y][next.z] = 1;
                    }
                if(now.z < n - now.y)
                {
                    next.x = now.x;
                    next.y = now.y + now.z;
                    next.z = 0;
                    if(!visit[next.x][next.y][next.z])
                    {
                        next.step = now.step + 1;
                        Q.push(next);
                        visit[next.x][next.y][next.z] = 1;
                    }
                }
                else
                {
                    next.x = now.x;
                    next.y = n;
                    next.z = now.z - (n - now.y);
                    if(!visit[next.x][next.y][next.z])
                    {
                        next.step = now.step + 1;
                        Q.push(next);
                        visit[next.x][next.y][next.z] = 1;
                    }
                }
            }
    
        }
        printf("NO
    ");
    }
    int main()
    {
        while(scanf("%d%d%d",&s,&n,&m)!=EOF&&n||m||s)
        {
            memset(visit,0,sizeof(visit));
            if(s%2==0)//s为奇数是不可能平分的,可优化。
                bfs();
            else
                printf("NO
    ");
        }
        return 0;
    }

    代码有点冗长啊,不过有很多是重复的,求更好的方法!欢迎评论

     

    转载请注明出处:http://www.cnblogs.com/yuyixingkong/ 自己命运的掌控着!
  • 相关阅读:
    关于游戏的配乐说法
    初识构建之法
    战略-战术-攻略
    《SRE google 运维解密》读书笔记1---第一部分
    什么,又是流程有问题?
    Scaling Memcache At Facebook
    技术博客开篇之言
    redis的一次迁移
    原生js--什么是闭包?
    原生js--事件绑定、事件监听器及事件解绑
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/3247359.html
Copyright © 2020-2023  润新知