#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
****************************************************************/
题目1457:非常可乐
最新推荐文章于 2022-04-11 00:15:00 发布