• HDU 1495 非常可乐


    非常可乐

    http://acm.hdu.edu.cn/showproblem.php?pid=1495

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2662    Accepted Submission(s): 1085

    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
     
    Author
    seeyou
     
    Source
     
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    struct node{
        int a[3];
        int step;
    }info;
    
    int hash[105][105][105];
    
    int BFS(int S,int N,int M){
        queue<node> myqueue;
        while(!myqueue.empty())
            myqueue.pop();
        int s[3];
        s[0]=S,s[1]=N,s[2]=M;
        memset(hash,0,sizeof(hash));
        hash[S][0][0]=1;
        info.a[0]=S,info.a[1]=0,info.a[2]=0,info.step=0;
        myqueue.push(info);
        int half=S/2;
        while(!myqueue.empty()){
            node tmp=myqueue.front();
            myqueue.pop();
            if((tmp.a[0]==half && tmp.a[1]==half) || (tmp.a[0]==half && tmp.a[2]==half) || (tmp.a[1]==half && tmp.a[2]==half))
                return tmp.step;
            tmp.step++;
            for(int i=0;i<3;i++)
                if(tmp.a[i]>0)
                    for(int j=0;j<3;j++){
                        if(i==j)
                            continue;
                        info=tmp;
                        if(info.a[i]<=s[j]-info.a[j]){
                            info.a[j]+=info.a[i];
                            info.a[i]=0;
                        }else{
                            info.a[i]-=(s[j]-info.a[j]);
                            info.a[j]=s[j];
                        }
                        if(!hash[info.a[0]][info.a[1]][info.a[2]]){
                            hash[info.a[0]][info.a[1]][info.a[2]]=1;
                            myqueue.push(info);
                        }
                    }
        }
        return 0;
    }
    
    int main(){
        int S,N,M;
        while(scanf("%d%d%d",&S,&N,&M)){
            if(S==0 && N==0 && M==0)
                break;
            if(S%2){
                printf("NO\n");
                continue;
            }
            int ans=BFS(S,N,M);
            if(ans)
                printf("%d\n",ans);
            else
                printf("NO\n");
        }
        return 0;
    }
  • 相关阅读:
    leetcode 451 根据字符出现频率排序
    leetcode 1833 雪糕的最大数量
    leetcode 166 Excel表列名称
    877 石子游戏
    01 背包问题
    正则表达式
    leetcode 160 相交链表
    git 备忘录
    leetcode 525 连续数组
    Mysite
  • 原文地址:https://www.cnblogs.com/jackge/p/2854050.html
Copyright © 2020-2023  润新知