进阶 非常可乐

题目链接

非常可乐
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
分6种情况

s倒m

s倒n

m倒n

m倒s

n倒m

n倒s

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>

using namespace std;

#define N 210
#define INF 0xfffffff
#define PI acos (-1.0)
#define EPS 1e-8

const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

struct node
{
    int x, y, all, t;        //x, y, all分别表示m, n, s杯中可乐的体积
}p, q;

int s, m, n, vis[N][N];
void BFS ();

int main ()
{
    while (cin >> s >> m >> n, s || m || n)
    {
        if (s % 2)
        {
            puts ("NO");
            continue;
        }
        if (m > n) swap (m, n);
        BFS ();
    }
    return 0;
}

void BFS ()
{
    memset (vis, 0, sizeof (vis));
    p.x = 0, p.y = 0, p.all = s, p.t = 0;
    queue <node> que;
    que.push(p);
    vis[p.x][p.y] = 1;

    while (que.size())
    {
        p = que.front(); que.pop();

        if (p.y == p.all && p.y == s / 2)
        {
            cout << p.t << endl;
            return;
        }

        if (p.x + p.all > m)                        //s倒m
        {
            q.x = m, q.y = p.y, q.all = p.all + p.x - m, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }
        else
        {
            q.x = p.x + p.all, q.y = p.y, q.all = 0, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }

        if (p.all + p.y > n)                       //s倒n
        {
            q.x = p.x, q.y = n, q.all = p.all + p.y - n, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }
        else
        {
            q.x = p.x, q.y = p.all + p.y, q.all = 0, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }

        if (p.x + p.y > n)                       //m倒n
        {
            q.x = p.x + p.y - n, q.y = n, q.all = p.all, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }
        else
        {
            q.x = 0, q.y = p.x + p.y, q.all = p.all, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }

        q.x = 0, q.y = p.y, q.all = p.x + p.all, q.t = p.t + 1;    //m倒s
        if (!vis[q.x][q.y])
        {
            vis[q.x][q.y] = 1;
            que.push(q);
        }

        if (p.x + p.y > m)                      //n倒m
        {
            q.x = m, q.y = p.x + p.y - m, q.all = p.all, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }
        else
        {
            q.x = p.x + p.y, q.y = 0, q.all = p.all, q.t = p.t + 1;
            if (!vis[q.x][q.y])
            {
                vis[q.x][q.y] = 1;
                que.push(q);
            }
        }

        q.x = p.x, q.y = 0, q.all = p.y + p.all, q.t = p.t + 1;    //n倒s
        if (!vis[q.x][q.y])
        {
            vis[q.x][q.y] = 1;
            que.push(q);
        }
    }
    puts ("NO");
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_大太阳_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值