zoukankan      html  css  js  c++  java
  • hdu1495 bfs搜索、模拟

    大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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;
    }


  • 相关阅读:
    go1.13 mod 实践和常见问题
    etcd 添加用户,授权特定目录
    golang 你所不知道的 log 和 fmt
    redis 原理系列之--字符串存储的实现原理(1)
    golang 写文件--详细解释
    面向对象范式的核心本质是?---不是继承 不是封装也不是多态
    关于自控力和拖延 的一点分享--《自控力》
    Linux 精确判断是否同一文件--及终端获取字符串md5 的值
    ARM版本及系列
    技术团队塑造
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/9692467.html
Copyright © 2011-2022 走看看