非常可乐(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
三个杯a,b,c相互倒水,一共6种状态:

a->b;

a->c;

b->a

b->c;

c->a;

c->b;

每种又分两种情况:

假设a->b;

一:a中水可以全倒在b中;

二:a中水不可以全倒在b中;

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <map>
#define INF 0x3f3f3f3f

using namespace std;

struct cup{
    int sum;
    int water;
}a, b, c;
struct node{
    int a_w, b_w, c_w;
    int step;
};
bool vis[105][105][105];
void bfs(){
    memset(vis, false, sizeof(vis));
    vis[c.water][a.water][b.water]=true;
    queue<node> q;
    node tem;
    tem.a_w=0;
    tem.b_w=0;
    tem.c_w=c.water;
    tem.step=0;
    q.push(tem);
    while(!q.empty()){
        tem=q.front();
        q.pop();
        if((tem.a_w==c.sum/2&&tem.b_w==c.sum/2) || (tem.a_w==c.sum/2&&tem.c_w==c.sum/2) || (tem.c_w==c.sum/2&&tem.b_w==c.sum/2)){//若有两个杯子中的水等于sum/2则水被均分;
            cout << tem.step << endl;
            return;
        }
        //a->b;
        if(tem.a_w<=b.sum-tem.b_w){
            a.water=0;
            b.water=tem.a_w+tem.b_w;
            if(!vis[tem.c_w][a.water][b.water]){
                vis[tem.c_w][a.water][b.water]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=b.water;
                tt.c_w=tem.c_w;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        else{
            a.water=tem.a_w-(b.sum-tem.b_w);
            b.water=b.sum;
            if(!vis[tem.c_w][a.water][b.water]){
                vis[tem.c_w][a.water][b.water]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=b.water;
                tt.c_w=tem.c_w;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        //a->c;
        if(tem.a_w<=(c.sum-tem.c_w)){
            a.water=0;
            c.water=tem.c_w+tem.a_w;
            if(!vis[c.water][a.water][tem.b_w]){
                vis[c.water][a.water][tem.b_w]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=tem.b_w;
                tt.c_w=c.water;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        else{
            a.water=tem.a_w-(c.sum-tem.c_w);
            c.water=c.sum;
            if(!vis[c.water][a.water][tem.b_w]){
                vis[c.water][a.water][tem.b_w]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=tem.b_w;
                tt.c_w=c.water;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        //b->a;
        if(tem.b_w<=(a.sum-tem.a_w)){
            b.water=0;
            a.water=tem.b_w+tem.a_w;
            if(!vis[tem.c_w][a.water][b.water]){
                vis[tem.c_w][a.water][b.water]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=b.water;
                tt.c_w=tem.c_w;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        else{
            b.water=tem.b_w-(a.sum-tem.a_w);
            a.water=a.sum;
            if(!vis[tem.c_w][a.water][b.water]){
                vis[tem.c_w][a.water][b.water]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=b.water;
                tt.c_w=tem.c_w;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        //b->c;
        if(tem.b_w<=(c.sum-tem.c_w)){
            b.water=0;
            c.water=tem.b_w+tem.c_w;
            if(!vis[c.water][tem.a_w][b.water]){
                vis[c.water][tem.a_w][b.water]=true;
                node tt;
                tt.c_w=c.water;
                tt.b_w=b.water;
                tt.a_w=tem.a_w;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        else{
            b.water=tem.b_w-(c.sum-tem.c_w);
            c.water=c.sum;
            if(!vis[c.water][tem.a_w][b.water]){
                vis[c.water][tem.a_w][b.water]=true;
                node tt;
                tt.a_w=tem.a_w;
                tt.b_w=b.water;
                tt.c_w=c.water;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        //c->a;
        if(tem.c_w<=(a.sum-tem.a_w)){
            c.water=0;
            a.water=tem.c_w+tem.a_w;
            if(!vis[c.water][a.water][tem.b_w]){
                vis[c.water][a.water][tem.b_w]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=tem.b_w;
                tt.c_w=c.water;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        else{
            c.water=tem.c_w-(a.sum-tem.a_w);
            a.water=a.sum;
            if(!vis[c.water][a.water][tem.b_w]){
                vis[c.water][a.water][tem.b_w]=true;
                node tt;
                tt.a_w=a.water;
                tt.b_w=tem.b_w;
                tt.c_w=c.water;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        //c->b;
        if(tem.c_w<=(b.sum-tem.b_w)){
            c.water=0;
            b.water=tem.b_w+tem.c_w;
            if(!vis[c.water][tem.a_w][b.water]){
                vis[tem.c_w][tem.a_w][b.water]=true;
                node tt;
                tt.a_w=tem.a_w;
                tt.b_w=b.water;
                tt.c_w=c.water;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
        else{
            c.water=tem.c_w-(b.sum-tem.b_w);
            b.water=b.sum;
            if(!vis[c.water][tem.a_w][b.water]){
                vis[c.water][tem.a_w][b.water]=true;
                node tt;
                tt.a_w=tem.a_w;
                tt.b_w=b.water;
                tt.c_w=c.water;
                tt.step=tem.step+1;
                q.push(tt);
            }
        }
    }
    cout << "NO\n";
    return;
}
int main(){

    int S, N, M;
    while(cin >> S >> N >> M, S&&N&&M){
        a.sum=N;
        b.sum=M;
        c.sum=S;
        c.water=S;
        a.water=b.water=0;
        if(S%2) cout << "NO\n";
        else bfs();
    }

    return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值