题目1457:非常可乐

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <map>
#include <functional>
#include <set>
using namespace std;
//1457
struct N{
    int a,b,c,t;
};
queue<N> Q;//队列
bool mark[101][101][101];
void AtoB(int &a,int sa,int &b,int sb){
    if(sb-b>=a){
        b+=a;a=0;
    }
    else{
        a-=sb-b;b=sb;
    }
}
int BFS(int s,int n,int m){
    while(!Q.empty()){
        N now=Q.front();
        Q.pop();
        int a,b,c;
        a=now.a;
        b=now.b;
        c=now.c;
        AtoB(a,s,b,n);  //由a倾倒向b
        if(mark[a][b][c]==false){   //若该体积尚未出现
            mark[a][b][c]=true;     //标记该体积
            N tmp;
            tmp.a=a;tmp.b=b;tmp.c=c;
            tmp.t=now.t+1;//生成新的状态
            if(a==s/2&&b==s/2)  return tmp.t;
            if(a==s/2&&c==s/2)  return tmp.t;
            if(b==s/2&&c==s/2)  return tmp.t;
            Q.push(tmp);
        }
        a=now.a;
        b=now.b;
        c=now.c;
        AtoB(b,n,a,s);//由b倒向a
        if(mark[a][b][c]==false){
            mark[a][b][c]=true;
            N tmp;
            tmp.a=a;
            tmp.b=b;
            tmp.c=c;
            tmp.t=now.t+1;
            if(a==s/2&&b==s/2)  return tmp.t;
            if(c==s/2&&b==s/2)  return tmp.t;
            if(a==s/2&&c==s/2)  return tmp.t;
            Q.push(tmp);
        }
        a=now.a;
        b=now.b;
        c=now.c;
        AtoB(a,s,c,m);//由a倒向c
        if(mark[a][b][c]==false){
            mark[a][b][c]=true;
            N tmp;
            tmp.a=a;
            tmp.b=b;
            tmp.c=c;
            tmp.t=now.t+1;
            if(a==s/2 && b==s/2)    return tmp.t;
            if(c==s/2 && b==s/2)    return tmp.t;
            if(a==s/2 && c==s/2)    return tmp.t;
            Q.push(tmp);
        }
        a=now.a;
        b=now.b;
        c=now.c;
        AtoB(c,m,a,s);//由c倒向a
        if(mark[a][b][c]==false){
            mark[a][b][c]=true;
            N tmp;
            tmp.a=a;
            tmp.b=b;
            tmp.c=c;
            tmp.t=now.t+1;
            if(a==s/2 && b==s/2)    return tmp.t;
            if(c==s/2 && b==s/2)    return tmp.t;
            if(a==s/2 && c==s/2)    return tmp.t;
            Q.push(tmp);
        }
        a=now.a;
        b=now.b;
        c=now.c;
        AtoB(b,n,c,m);//b to c
        if(mark[a][b][c]==false){
            mark[a][b][c]=true;
            N tmp;
            tmp.a=a;
            tmp.b=b;
            tmp.c=c;
            tmp.t=now.t+1;
            if(a==s/2 && b==s/2) return tmp.t;
            if(c==s/2 && b==s/2) return tmp.t;
            if(a==s/2 && c==s/2) return tmp.t;
            Q.push(tmp);
        }
        a=now.a;
        b=now.b;
        c=now.c;
        AtoB(c,m,b,n);//c倒向b
        if(mark[a][b][c]==false){
            mark[a][b][c]=true;
            N tmp;
            tmp.a=a;
            tmp.b=b;
            tmp.c=c;
            tmp.t=now.t+1;
            if(a==s/2 && b==s/2) return tmp.t;
            if(c==s/2 && b==s/2) return tmp.t;
            if(a==s/2 && c==s/2) return tmp.t;
            Q.push(tmp);
        }
    }
    return -1;
}
int main(){
    int s,n,m;
    //freopen("input.txt","r",stdin);
    while(scanf("%d%d%d",&s,&n,&m)!=EOF){
        if(s==0)    break;
        if(s%2==1)  {puts("NO");continue;}
        for(int i=0;i<=s;i++){
            for(int j=0;j<=n;j++){
                for(int k=0;k<=m;k++){
                    mark[i][j][k]=false;
                }
            }
        }
        N tmp;
        tmp.a=s; tmp.b=0; tmp.c=0; tmp.t=0;
        while(!Q.empty())   Q.pop();//清空队列
        Q.push(tmp);
        mark[s][0][0]=true;
        int rec=BFS(s,n,m);
        if(rec==-1) puts("NO");
        else        printf("%d\n",rec);
    }
    return 0;
}
/**************************************************************
    Problem: 1457
    User: cust123
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:2528 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值