M - 非常可乐HDU1495 BFS


M - 非常可乐
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice 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

//找到状态,并表示,我是以(s,n,m)为状态,之间的倒水关系为边,进行BFS
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=105;
const int inf=200000;
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define For(i,n) for(int i=0;i<(n);i++)
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32);
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return x;
}
template<class T> inline void read_(T&x,T&y)
{
    read(x);
    read(y);
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//  -------IO template------
struct node
{
    int s,n,m;
    node(int a,int b,int c)
    {
        s=a;n=b;m=c;
    }
};
int N,M,S;
bool ok(node a)
{
    int cnt=0;
    if(a.s==S/2)
        cnt++;
    if(a.n==S/2)
        cnt++;
    if(a.m==S/2)
        cnt++;
    if(cnt>=2)return true;
    return  false;
}

int vis[maxn][maxn][maxn];
bool flag=0;
void bfs(int s,int n,int m)
{
    queue<node> q;
    q.push(node(s,n,m));
    vis[s][n][m]=1;
    memset(vis,0,sizeof(vis));
    while(!q.empty())
    {
        node tmp=q.front();q.pop();
       // printf("%d %d %d\n",tmp.s,tmp.n,tmp.m);
        if(ok(tmp))
        {
            printf("%d\n",vis[tmp.s][tmp.n][tmp.m]);
            flag=false;
            return ;
        }
        ///s->n
        if(tmp.s>0&&tmp.n<N)
        {
            int t=min(N-tmp.n,tmp.s);
            if(!vis[tmp.s-t][tmp.n+t][tmp.m])
            {
                q.push(node(tmp.s-t,tmp.n+t,tmp.m));
                vis[tmp.s-t][tmp.n+t][tmp.m]=vis[tmp.s][tmp.n][tmp.m]+1;
            }
        }
        ///s->m
        if(tmp.s>0&&tmp.m<M)
        {
            int t=min(tmp.s,M-tmp.m);
            if(!vis[tmp.s-t][tmp.n][tmp.m+t])
            {
                q.push(node(tmp.s-t,tmp.n,tmp.m+t));
                vis[tmp.s-t][tmp.n][tmp.m+t]=vis[tmp.s][tmp.n][tmp.m]+1;
            }
        }

        ///n->s
        if(tmp.n>0&&tmp.s<S)
        {
            int t=min(tmp.n,S-tmp.s);
            if(!vis[tmp.s+t][tmp.n-t][tmp.m])
            {
                q.push(node(tmp.s+t,tmp.n-t,tmp.m));
                vis[tmp.s+t][tmp.n-t][tmp.m]=vis[tmp.s][tmp.n][tmp.m]+1;
            }
        }
        ///n->m
        if(tmp.n>0&&tmp.m<M)
        {
            int t=min(tmp.n,M-tmp.m);
            if(!vis[tmp.s][tmp.n-t][tmp.m+t])
            {
                q.push(node(tmp.s,tmp.n-t,tmp.m+t));
                vis[tmp.s][tmp.n-t][tmp.m+t]=vis[tmp.s][tmp.n][tmp.m]+1;
            }
        }
        ///m->s
        if(tmp.s<S&&tmp.m>0)
        {
            int t=min(tmp.m,S-tmp.s);
            if(!vis[tmp.s+t][tmp.n][tmp.m-t])
            {
                q.push(node(tmp.s+t,tmp.n,tmp.m-t));
                vis[tmp.s+t][tmp.n][tmp.m-t]=vis[tmp.s][tmp.n][tmp.m]+1;
            }
        }
        ///m->n
        if(tmp.m>0&&tmp.n<N)
        {
            int t=min(tmp.m,N-tmp.n);
            if(!vis[tmp.s][tmp.n+t][tmp.m-t])
            {
                q.push(node(tmp.s,tmp.n+t,tmp.m-t));
                vis[tmp.s][tmp.n+t][tmp.m-t]=vis[tmp.s][tmp.n][tmp.m]+1;
            }
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int i,j,k,t;
    while(~scanf("%d%d%d",&S,&N,&M)&&(S||N||M))
    {
        flag=true;
        if(S&1){printf("NO\n");continue;}
        bfs(S,0,0);
        if(flag)printf("NO\n");
    }
    return 0;
}

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值