zoukankan      html  css  js  c++  java
  • HDU 1495 非常可乐【BFS/倒水问题】

    非常可乐
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 22067 Accepted Submission(s): 8968

    Problem 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

    【题意】:给三个数字 s n m s=n+m s在1到100之间
    就是个倒水问题,可以互相倒来倒去,一共有六种倒法,现在要求最少经过多少步就能平分那么多水 。
    【分析】:之前因为min的顺序没写到最前面导致ed没有初始化,以及120行有个笔误把st写成ed
    【代码】:

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 500; //数组之前开小了...但是一直显示TLE而不是RE???我爆哭
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0};
    const int dy[] = {0,0,1,-1};
    int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int a,b,c;
    int v[105][105][105];
    
    struct node
    {
        int x,y,z,step;
    }st,ed;
    int check(int x,int y,int z)
    {
        return (x==a/2 && y==a/2) || (x==a/2 && z==a/2) || (y==a/2 && z==a/2);
    }
    queue<node> q;
    void bfs()
    {
        ms(v,0);
        while(!q.empty()) q.pop();
        //初始化三个杯子:可乐杯满的;b、c杯空的
        st.x = a;//可乐杯子满的
        st.y = 0;//b杯子空
        st.z = 0;//c杯子空
        st.step = 0;
        v[a][0][0] = 1; //标记杯子状态
        q.push(st);
        while(!q.empty())
        {
            st = q.front();
            q.pop();
            if(check(st.x,st.y,st.z)) //满足条件直接输出
            {
                printf("%d
    ",st.step);
                return ;
            }
    
            //a——>b
            if(st.x>0 && st.y<b)
            {
                //两种情况,一种是把A里的水全倒到B还没有把B倒满
                //另一种是,A里的水还没倒完B就满了,所以取两者最小值
                ed.y = min(b,st.x+st.y);
                ed.x = st.x - (ed.y-st.y);
                ed.z = st.z;
                ed.step = st.step + 1;
                if(!v[ed.x][ed.y][ed.z])
                {
                    v[ed.x][ed.y][ed.z]=1;
                    q.push(ed);
                }
            }
            //a——>c
            if(st.x>0 && st.z<c)
            {
                ed.z = min(c,st.x+st.z);
                ed.x = st.x - (ed.z-st.z);
                ed.y = st.y;
                ed.step = st.step + 1;
                if(!v[ed.x][ed.y][ed.z])
                {
                    v[ed.x][ed.y][ed.z]=1;
                    q.push(ed);
                }
            }
            //b——>a
            if(st.y>0 && st.x<a)
            {
                ed.x = min(a,st.x+st.y);
                ed.y = st.y - (ed.x-st.x);
                ed.z = st.z;
                ed.step = st.step + 1;
                if(!v[ed.x][ed.y][ed.z])
                {
                    v[ed.x][ed.y][ed.z]=1;
                    q.push(ed);
                }
            }
            //b——>c
            if(st.y>0 && st.z<c)
            {
                ed.z = min(c,st.y+st.z);
                ed.x = st.x;
                ed.y = st.y - (ed.z-st.z);
                ed.step = st.step + 1;
                if(!v[ed.x][ed.y][ed.z])
                {
                    v[ed.x][ed.y][ed.z]=1;
                    q.push(ed);
                }
            }
            //c——>a
            if(st.z>0 && st.x<a)
            {
                ed.x = min(a,st.x+st.z);
                ed.y = st.y;
                ed.z = st.z - (ed.x-st.x);
                ed.step = st.step + 1;
                if(!v[ed.x][ed.y][ed.z])
                {
                    v[ed.x][ed.y][ed.z]=1;
                    q.push(ed);
                }
            }
            //c——>b
            if(st.z>0 && st.y<b)
            {
                ed.y = min(b,st.y+st.z);
                ed.x = st.x;
                ed.z = st.z - (ed.y-st.y);
                ed.step = st.step + 1;
                if(!v[ed.x][ed.y][ed.z])
                {
                    v[ed.x][ed.y][ed.z]=1;
                    q.push(ed);
                }
            }
        }
        printf("NO
    ");
    }
    int main()
    {
        while(~scanf("%d%d%d",&a,&b,&c),a+b+c)
        {
            while(!q.empty()) q.pop();
            if(a&1) {puts("NO"); continue;}
            bfs();
        }
    }
    /*
    7 4 3
    4 1 3
    0 0 0
    Sample Output
    NO
    3
    */
    

    【可以使用数组装x y z,把倒水逻辑封装起来,就不要写6次只要写1次】

    #include<bits/stdc++.h>
    using namespace std;
    #define ms(a,b) memset(a,b,sizeof(a))
    int v[105][105][105];
    int c[5];
    struct node
    {
        int c[5];
        int s;
    }tmp,temp;
    void pour(int a,int b)
    {
        int sum = temp.c[a] + temp.c[b];
        if(sum>=c[b])
             temp.c[b]=c[b];
         else
             temp.c[b]=sum;
        temp.c[a] = sum - temp.c[b];
    }
    
    int ok(int x,int y,int z)
    {
        return (x==c[0]/2 && y==c[0]/2) || (x==c[0]/2 && z==c[0]/2) || (y==c[0]/2 && z==c[0]/2);
    }
    
    void bfs()
    {
        ms(v,0);
        queue<node> q;
        tmp.c[0]=c[0];
        tmp.c[1]=tmp.c[2]=tmp.s=0;
        v[c[0]][0][0]=1;
        q.push(tmp);
        while(!q.empty())
        {
            tmp = q.front();
            q.pop();
            if(ok(tmp.c[0],tmp.c[1],tmp.c[2]))
            {
                printf("%d
    ",tmp.s);
                return ;
            }
            for(int i=0;i<3;i++)     //枚举倒水c0 、c1、c2代表三个杯子
            {
                if(tmp.c[i]>0)        //不能倒完
                {
                    for(int j=0;j<3;j++)     //枚举接水
                    {
                        if(i==j) continue;     //不能自己倒给自己!所以筛出6种状态(9种去掉0+0、1+1、2+2)
                        temp = tmp;         //中间变量保存原始值,因为是平行而非覆盖
                        pour(i,j);              //i——>j 变量要和temp保持一致,名字不能是别的
                        if(!v[temp.c[0]][temp.c[1]][temp.c[2]])
                        {
                            temp.s++;
                            v[temp.c[0]][temp.c[1]][temp.c[2]] = 1;
                            q.push(temp);
                        }
                    }
                }
            }
        }
        printf("NO
    ");
    }
    int main()
    {
        while(~scanf("%d%d%d",&c[0],&c[1],&c[2]), c[0]+c[1]+c[2])
        {
            if(c[0]&1)
            {
                printf("NO
    ");
                continue;
            }
            bfs();
        }
    }
    
  • 相关阅读:
    P1168 中位数(对顶堆)
    P2341 [HAOI2006]受欢迎的牛
    P1967 货车运输
    树状数组的神操作QAQ
    P1063 能量项链
    P1429 平面最近点对(加强版)
    P2571 [SCOI2010]传送带
    4 Values whose Sum is 0
    UVA529 Addition Chains
    UVA307 Sticks
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9297575.html
Copyright © 2011-2022 走看看