非常可乐(HDU-1495)

非常可乐

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


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
 

Recommend
LL

 解题思路: 此题就是用广搜,去每次遍历6种操作,然后遇到满足条件的就退出。

 

这里用一种代码特别简短的方法,证明有点复杂。先贴出来吧。

 

#include<cstdio>
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
    int a,b,c;
    while(scanf("%d%d%d",&a,&b,&c),a+b+c)
    {
        a/=gcd(b,c);
        if(a&1)printf("NO\n");
        else printf("%d\n",a-1);
    }
    return 0;
}

再看看,用广搜去做的吧


#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
struct s{
    int a;
    int b;
    int c;
    int step;
};
int book1[110][110][110];
int main()
{
    int v,n,m;
    while(scanf("%d%d%d",&v,&n,&m)!=EOF)
    {
        memset(book1,0,sizeof(book1));
        if(v==0&&n==0&&m==0)
            return 0;
    queue<s>q;
    s st;
    st.a=v;
    st.b=0;
    st.c=0;
    st.step=0;
    q.push(st);
    int ans=0;
    if(v%2!=1)
    {
    while(!q.empty())
    {
        s temp=q.front();

        q.pop();

        if(book1[temp.a][temp.b][temp.c]==1)
        {
            continue;
        }
        else
        {
                //printf("%d %d %d\n",temp.a,temp.b,temp.c);
            book1[temp.a][temp.b][temp.c]=1;
        if((temp.c==temp.b&&temp.a==0)||(temp.a==temp.b&&temp.c==0)||(temp.c==temp.a&&temp.b==0))
        {
            ans=temp.step;
            break;
        }
        for(int i=0;i<6;i++)
        {
            s tmp;
            if(i==0&&temp.a!=0)
            {
                if(temp.a>(n-temp.b))
                {
                    tmp.b=n;
                    tmp.c=temp.c;
                    tmp.a=temp.a-(n-temp.b);
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
                else
                {
                    tmp.b=temp.a+temp.b;
                    tmp.a=0;
                    tmp.c=temp.c;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
            }
            else if(i==1&&temp.a!=0)
            {
               if(temp.a>(m-temp.c))
                {
                    tmp.b=temp.b;
                    tmp.c=m;
                    tmp.a=temp.a-(m-temp.c);
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
                else
                {
                    tmp.b=temp.b;
                    tmp.a=0;
                    tmp.c=temp.a+temp.c;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
            }
             else if(i==2&&temp.b!=0)
            {

               if(temp.b>(v-temp.a))
                {
                    tmp.b=temp.b-(v-temp.a);
                    tmp.c=tmp.c;
                    tmp.a=v;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
                else
                {
                    tmp.b=0;
                    tmp.a=temp.a+temp.b;
                    tmp.c=temp.c;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
            }
            else if(i==3&&temp.b!=0)
            {

               if(temp.b>(m-temp.c))
                {
                    tmp.b=temp.b-(m-temp.c);
                    tmp.c=m;
                    tmp.a=temp.a;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
                else
                {
                    tmp.b=0;
                    tmp.a=temp.a;
                    tmp.c=temp.c+temp.b;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
            }
            else if(i==4&&temp.c!=0)
            {

               if(temp.c>(n-temp.b))
                {
                    tmp.b=n;
                    tmp.c=temp.c-(n-temp.b);
                    tmp.a=temp.a;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
                else
                {
                    tmp.b=temp.b+temp.c;
                    tmp.a=temp.a;
                    tmp.c=0;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
            }
            else if(i==5&&temp.c!=0)
            {

               if(temp.c>(v-temp.a))
                {
                    tmp.b=temp.b;
                    tmp.c=temp.c-(v-temp.a);
                    tmp.a=v;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
                else
                {
                    tmp.b=temp.b;
                    tmp.a=temp.a+temp.c;
                    tmp.c=0;
                    tmp.step=temp.step+1;
                    q.push(tmp);
                }
            }

        }
        }
    }
    }
    if(ans!=0)
    printf("%d\n",ans);
    else
        printf("NO\n");
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值