#include<bits/stdc++.h>
using namespace std;
const int maxn=107;
int v[3],s[3],vis[maxn][maxn][maxn];//vis看这个状态遇到过没
struct ss{
int step;
int s[3];
};
//check是在while里for外面
int check(int x,int y,int z){
if(x==y&&z==0||x==z&&y==0||z==y&&x==0) return 1;
return 0;
}
void bfs(){
memset(vis,0,sizeof(vis));
queue<ss>q ;ss cur,tem;
cur.step=0;
cur.s[0]=v[0];cur.s[1]=cur.s[2]=0;
q.push(cur);
vis[v[0]][0][0]=1;
//while的每一个状态都有3*3种可能的倒法到下一状态
while(!q.empty()){
cur=q.front();q.pop();
if(check(cur.s[0],cur.s[1],cur.s[2])){
cout<<cur.step<<endl; return;
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(i!=j){//不能自己倒给自己
tem=cur;tem.step=cur.step+1;
//若自己是0 没法给别人倒;若满 不需要倒入
if(cur.s[i]>0&&(v[j]-cur.s[j])>0){
if(cur.s[i]-(v[j]-cur.s[j])>0){
tem.s[i]=cur.s[i]-(v[j]-cur.s[j]);
tem.s[j]=v[j];
}
else{
tem.s[i]=0;
tem.s[j]=cur.s[i]+cur.s[j];
}
if(!vis[tem.s[0]][tem.s[1]][tem.s[2]]){
vis[tem.s[0]][tem.s[1]][tem.s[2]]=1;
q.push(tem);
}
}
}
}
}
}
cout<<"NO"<<endl; return;
}
int main(){
while(cin>>v[0]>>v[1]>>v[2]){
if(v[0]==0&&v[1]==0&&v[2]==0) break;
if(v[0]%2==1) cout<<"NO"<<endl;
else bfs();
}
}
hdu1495
最新推荐文章于 2022-12-30 12:00:55 发布