hdu_1495_非常可乐(bfs模拟)

7 篇文章 0 订阅

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495

题意:不解释

题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长,比较重复,尝试优化了一下,不过WA了难过

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct dt{int s,m,n,t;};
int s,m,n;
bool v[101][101][101];
int bfs(){
	memset(v,0,sizeof(v));
	v[s][0][0]=1;
	dt tmp,o;tmp.s=s,tmp.m=0,tmp.n=0,tmp.t=0;
	queue<dt>Q;Q.push(tmp);
	while(!Q.empty()){
		o=Q.front();Q.pop();
		if((o.s==o.m&&!o.n)||(o.s==o.n&&!o.m)||(o.n==o.m&&!o.s))return o.t;
		if(o.s){
			if(o.m<m){
				if(o.s+o.m>=m){
					tmp.m=m,tmp.s=o.s+o.m-m,tmp.n=o.n,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}else{
					tmp.m=o.m+o.s,tmp.s=0,tmp.n=o.n,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}
			}
			if(o.n<n){
				if(o.s+o.n>=n){
					tmp.n=n,tmp.s=o.s+o.n-n,tmp.m=o.m,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}else{
					tmp.n=o.n+o.s,tmp.s=0,tmp.m=o.m,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
					}
			}
		}
		if(o.m){
			if(o.s<s){
				if(o.s+o.m>=s){
					tmp.s=s,tmp.m=o.m+o.s-s,tmp.n=o.n,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}else{
					tmp.s=o.s+o.m,tmp.m=0,tmp.n=o.n,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}
			}
			if(o.n<n){
				if(o.m+o.n>=n){
					tmp.n=n,tmp.m=o.m+o.n-n,tmp.s=o.s,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}else{
					tmp.n=o.n+o.m,tmp.m=0,tmp.s=o.s,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}
			}
		}
		if(o.n){
			if(o.m<m){
				if(o.n+o.m>=m){
					tmp.m=m,tmp.n=o.n+o.m-m,tmp.s=o.s,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}else{
					tmp.m=o.m+o.n,tmp.n=0,tmp.s=o.s,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}
			}
			if(o.s<s){
				if(o.n+o.s>=s){
					tmp.s=s,tmp.n=o.n+o.s-s,tmp.m=o.m,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}else{
					tmp.s=o.s+o.n,tmp.n=0,tmp.m=o.m,tmp.t=o.t+1;
					if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
				}
			}
		}
	}
	return -1;
}
int main(){
	while(~scanf("%d%d%d",&s,&m,&n),s+n+m){
		if(s%2){puts("NO");continue;}
		int ans=bfs();
		if(ans==-1)puts("NO");
		else printf("%d\n",ans);
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值