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\n",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\n");
}
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
*/

猜你喜欢

转载自www.cnblogs.com/Roni-i/p/9297575.html