zoukankan      html  css  js  c++  java
  • HDU 1495 非常可乐

    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
     
    题意:中文题,不用多说,三个杯子可以互相倒可乐,然而并没有刻度,那么只能依靠杯子的总容量来算了,首先确定当一个杯子有可乐时就可以向其他两个杯子倒可乐了,那么只要判断另外两个杯子的状态就行了,每次像其他杯子倒水都算作一次(可以确定有六种可能的状态了),剩下的就是一个BFS了,就是有点麻烦,代码可长。。。。
    #include<stdio.h>
    #include<queue>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    const int maxn=110;
    
    struct node
    {
        int s, n, m, k; ///s,n,m分别表示可乐瓶和两个杯子中可乐的容量,k表示倒可乐的次数
    };
    int S, N, M, vis[maxn][maxn][maxn];
    
    int BFS()
    {
        node now, next;
        queue<node>Q;
    
        now.s = S;
        now.n = now.m = now.k = 0;
    
        Q.push(now);
        vis[now.s][now.n][now.m] = 1;
    
        while (!Q.empty())
        {
            now = Q.front(); Q.pop();
    
            if ((now.s==S/2 && now.n==S/2) || (now.s==S/2 && now.m==S/2) || (now.n==S/2 && now.m==S/2))
                return now.k; ///题中要求只要能平分就行,所以不管哪两个杯子装的是平分的可乐都行
            ///接下来就是三个杯子分别有可乐的情况,每种情况中又分别判断了另两个杯子的状态,所以一共是六种
            if (now.s != 0) 
            {
                if (now.s <= N-now.n)
                {
                    next.s = 0;
                    next.n = now.n+now.s;
                    next.m = now.m;
                    next.k = now.k+1;
                }
                else
                {
                    next.s = now.s-(N-now.n);
                    next.n = N;
                    next.m = now.m;
                    next.k = now.k+1;
                }
                if (!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m] = 1;
                    Q.push(next);
                }
    
                if (now.s <= M-now.m)
                {
                    next.s = 0;
                    next.n = now.n;
                    next.m = now.m+now.s;
                    next.k = now.k+1;
                }
                else
                {
                    next.s = now.s-(M-now.m);
                    next.n = now.n;
                    next.m = M;
                    next.k = now.k+1;
                }
                if (!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m] = 1;
                    Q.push(next);
                }
            }
    
            if (now.n != 0)
            {
                if (now.n <= S-now.s)
                {
                    next.s = now.s+now.n;
                    next.n = 0;
                    next.m = now.m;
                    next.k = now.k+1;
                }
                else
                {
                    next.s = S;
                    next.n = now.n-(S-now.s);
                    next.m = now.m;
                    next.k = now.k+1;
                }
                if (!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m] = 1;
                    Q.push(next);
                }
    
                if (now.n <= M-now.m)
                {
                    next.s = now.s;
                    next.n = 0;
                    next.m = now.m+now.n;
                    next.k = now.k+1;
                }
                else
                {
                    next.s = now.s;
                    next.n = now.n-(M-now.m);
                    next.m = M;
                    next.k = now.k+1;
                }
                if (!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m] = 1;
                    Q.push(next);
                }
            }
    
            if (now.m != 0)
            {
                if (now.m <= S-now.s)
                {
                    next.s = now.s+now.m;
                    next.n = now.n;
                    next.m = 0;
                    next.k = now.k+1;
                }
                else
                {
                    next.s = S;
                    next.n = now.n;
                    next.m = now.m-(S-now.s);;
                    next.k = now.k+1;
                }
                if (!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m] = 1;
                    Q.push(next);
                }
    
                if (now.m <= N-now.n)
                {
                    next.s = now.s;
                    next.n = now.m+now.n;
                    next.m = 0;
                    next.k = now.k+1;
                }
                else
                {
                    next.s = now.s;
                    next.n = N;
                    next.m = now.m-(N-now.n);
                    next.k = now.k+1;
                }
                if (!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m] = 1;
                    Q.push(next);
                }
            }
        }
    
        return -1;
    }
    
    int main ()
    {
        int ans;
    
        while (scanf("%d%d%d", &S, &N, &M), S+N+M)
        {
            memset(vis, 0, sizeof(vis));
    
            if (S % 2 != 0) printf("NO
    "); ///当然可乐的容量原本就是奇数,是肯定不会被平分的
            else
            {
                ans = BFS();
    
                if (ans == -1) printf("NO
    ");
                else printf("%d
    ", ans);
            }
        }
    
        return 0;
    }
  • 相关阅读:
    常用Git命令清单
    上海金瑢信息有限公司面试
    上海视频面试
    bootstrp-3.0
    B站小姐姐面试分享2
    B站小姐姐分享第一次电话面试
    Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组。
    findIndex
    es5,es6
    es6数组去重
  • 原文地址:https://www.cnblogs.com/syhandll/p/4828503.html
Copyright © 2011-2022 走看看