您的位置:首页 > 其它

进阶 非常可乐

2015-07-17 10:38 302 查看
题目链接

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

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

;
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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: