hdu1495

#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();
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值