HDU 1495 非常可乐 (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

题意

题解

dfs不好控制到目标状态的最少步数,所以选择BFS,因为杯子没有刻度,倒水只能从一个杯子倒入另一个杯子,要么把自己倒完,要么把另一个杯子倒满。那么下一步的状态有六种(六种倒法),当某一个杯子的水量是s/2,次数为d时,如果另外两个杯子有一个为空的时候(剩下一个肯定也是s/2),即为终点状态。否则就需要再倒一次,答案就是d+1。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1005;
typedef long long LL;
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

struct node
{
    int a,b,c,d;
    node(){}
    node(int a,int b,int c,int d):a(a),b(b),c(c),d(d){}
}q[1000005];
bool vis[105][105][105];
int l,r,s,n,m;
void pour(int a,int b,int c,int d)
{
    if(!vis[a][b][c])
    {
        vis[a][b][c]=true;
        q[r++]=node(a,b,c,d+1);
    }
}
int bfs()
{
    memset(vis,false,sizeof(vis));
    l=r=0;
    int a,b,c,d;
    q[r++]=node(s,0,0,0);
    while(l<r)
    {
        a=q[l].a,b=q[l].b,c=q[l].c,d=q[l++].d;
        if(a==s/2||b==s/2||c==s/2)
            return d+(a&&b&&c!=0);
        pour(a-m+b,m,c,d);//s->m
        pour(a-n+c,b,n,d);//s->n
        pour(a+b,0,c,d);//m->s
        pour(a+c,b,0,d);//n->s
        if(b>n-c)//m->n 判断能不能把m杯中的水全部倒进n中
            pour(a,b-n+c,n,d);
        else
            pour(a,0,c+b,d);
        if(c>m-b)//n->m 同理
            pour(a,m,c-m+b,d);
        else
            pour(a,b+c,0,d);
    }
    return 0;
}
int main()
{
    while(cin>>s>>n>>m,s||n||m)
    {
        if(s%2)//奇数肯定不能平分
        {
            cout<<"NO"<<endl;
            continue;
        }
        int ans=bfs();
        if(!ans)
            cout<<"NO"<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}
posted @ 2018-03-11 10:06  Zireael  阅读(168)  评论(0编辑  收藏  举报