HDU - 1495 非常可乐 BFS

HDU - 1495 非常可乐

思路:

可以相互倒可乐,所以有6中方式。将每一种方式列举出来就行。但是判断是否均分时需要让容量小的杯子中没有可乐,即可乐瓶中的可乐 和 容量大的杯子中的可乐体积相同。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int vis[110][110];
int s,n,m,ans,k;
struct node
{
    int sum;  //在这个状态下可乐瓶中剩余的可乐
    int x,y;  //两个杯子中可乐的容量
    int step;
};
void bfs(int x,int y)
{
    queue<node> q;
    node w,h;
    w.x = x;
    w.y = y;
    w.step = 0;
    w.sum = s;
    vis[w.x][w.y] = 1;
    q.push(w);
    while(!q.empty())
    {
        w = q.front();
        q.pop();
        for(int i=1;i<=6;i++)
        {
            if(i==1)    //把s倒入n中
            {
                if(w.sum > n-w.x)   //如果s中的水比n杯剩余的容量大
                {
                    h.x = n;
                    h.sum = w.sum-n;
                }
                else           //s中的水全部倒入n中
                {
                    h.x = w.x+w.sum;
                    h.sum = 0;
                }
                h.y = w.y;
            }
            else if(i==2)  //把s倒入m中
            {
                if(w.sum > m-w.y)   //s中的水比m杯空余的容量大
                {
                    h.y = m;
                    h.sum = w.sum-(m-w.y);
                }
                else
                {
                    h.y = w.y+w.sum;
                    h.sum = 0;
                }
                h.x = w.x;
            }
            else if(i==3)   //n->s
            {
                if(w.x > s-w.sum)
                {
                    h.sum = s;
                    h.x = w.x-(s-w.sum);
                }
                else
                {
                    h.x = 0;
                    h.sum = w.sum+w.x;
                }
                h.y = w.y;
            }
            else if(i==4)   //n->m
            {
                if(w.x > m-w.y)
                {
                    h.x = w.x-(m-w.y);
                    h.y = m;
                }
                else
                {
                    h.x = 0;
                    h.y = w.y+w.x;
                }
                h.sum = w.sum;
            }
            else if(i==5)   //m->s
            {
                if(w.y > s-w.sum)
                {
                    h.y = w.y-(s-w.sum);
                    h.sum = s;
                }
                else
                {
                    h.y = 0;
                    h.sum = w.sum+w.y;
                }
                h.x = w.x;
            }
            else if(i==6)   //m->n
            {
                if(w.y > n-w.x)
                {
                    h.x = n;
                    h.y = w.y-(n-w.x);
                }
                else
                {
                    h.x = w.x+w.y;
                    w.y = 0;
                }
                h.sum = w.sum;
            }
            if(vis[h.x][h.y])
                continue;
            vis[h.x][h.y] = 1;
            h.step = w.step+1;
            if(h.x==k && h.sum==k)  //那个小的杯子需要为空
            {
                ans = h.step;
                return ;
            }
            q.push(h);
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&s,&n,&m) && s+n+m)
    {
        ans = 0;
        memset(vis,0,sizeof(vis));
        if(s%2)
            printf("NO\n");
        else
        {
            if(n<m)    // n为容量大的杯子
                swap(n,m);
            k = s/2;
            bfs(0,0);
            if(ans)
                printf("%d\n",ans);
            else
                printf("NO\n");
        }
    }
    return 0;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值