码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1495 bfs搜索、模拟

时间:2018-09-23 15:02:08      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:模拟   input   ret   case   cout   efi   clu   运动   end   

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>

using namespace std;

typedef long long LL;
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemI(x) memset(x, -1, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x))

const int MAXN = 10005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

//vis -> all of case of n and m
int vis[150][150], s, n, m;
struct Node
{
    int s, n, m, cnt;
};
queue<Node> q;

int bfs()
{
    while(!q.empty())
        q.pop();
    Mem0(vis);
    Node now, next;
    now.s = s, now.n = 0, now.m = 0, now.cnt = 0;
    q.push(now);
    vis[now.n][now.m] = 1;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
//        cout << now.s << " " << now.n << " " << now.m << " " << now.cnt << endl;
        int cnt = 0;
        if(2 * now.s == s)
            cnt++;
        if(2 * now.n == s)
            cnt++;
        if(2 * now.m == s)
            cnt++;
        if(cnt == 2)
            return now.cnt;
        // s -> n
        next.cnt = now.cnt + 1;
        if(now.s && now.n != n)
        {
            int d = n - now.n;
            next.s = max(0, now.s - d);
            next.n = min(n, now.n + now.s);
            next.m = now.m;
            if(!vis[next.n][next.m])
            {
                vis[next.n][next.m] = 1;
                q.push(next);
            }
        }
        // s -> m
        if(now.s && now.m != m)
        {
            int d = m - now.m;
            next.s = max(0, now.s - d);
            next.m = min(m, now.m + now.s);
            next.n = now.n;
            if(!vis[next.n][next.m])
            {
                vis[next.n][next.m] = 1;
                q.push(next);
            }
        }
        // n -> s
        if(now.n && now.s != s)
        {
            int d = s - now.s;
            next.n = max(0, now.n - d);
            next.s = min(s, now.s + now.n);
            next.m = now.m;
            if(!vis[next.n][next.m])
            {
                vis[next.n][next.m] = 1;
                q.push(next);
            }
        }
        // n -> m
        if(now.n && now.m != m)
        {
            int d = m - now.m;
            next.n = max(0, now.n - d);
            next.m = min(m, now.m + now.n);
            next.s = now.s;
            if(!vis[next.n][next.m])
            {
                vis[next.n][next.m] = 1;
                q.push(next);
            }
        }
        //m -> s
        if(now.m && now.s != s)
        {
            int d = s - now.s;
            next.m = max(0, now.m - d);
            next.s = min(s, now.s + now.m);
            next.n = now.n;
            if(!vis[next.n][next.m])
            {
                vis[next.n][next.m] = 1;
                q.push(next);
            }
        }
        // m -> n
        if(now.m && now.n != n)
        {
            int d = n - now.n;
            next.m = max(0, now.m - d);
            next.n = min(n, now.n + now.m);
            next.s = now.s;
            if(!vis[next.n][next.m])
            {
                vis[next.n][next.m] = 1;
                q.push(next);
            }
        }
    }
    return 0;
}

int main()
{
    while(cin >> s >> n >> m)
    {
        if(!s && !n && !m)
            break;
        if(s % 2)
        {
            cout << "NO" << endl;
            continue;
        }
        else
        {
            int ans = bfs();
            if(ans)
                cout << ans << endl;
            else
                cout << "NO" << endl;
        }
    }
    return 0;
}

 



hdu1495 bfs搜索、模拟

标签:模拟   input   ret   case   cout   efi   clu   运动   end   

原文地址:https://www.cnblogs.com/shuizhidao/p/9692467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!