hdu 1495Legal or Not(搜索+模拟)

刚开始我以为这是一道类似于博弈的题目,还在那儿思考有没有什么办法直接推出答案,看了别人的代码才知道这题没啥好办法,老老实实的模拟才是王道。

应该说是很有意思的搜索,每次根据之前的三个杯子的不同状态产生下一个状态,一直到搜索到结果或者返回-1为止。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 101
int map[N][N][N];
int x,y,z;
struct node
{
	int x,y,z;
	int step;
};
int BFS()
{
	queue<node>q;
	node cur,next;
	cur.x=x;
	cur.y=0;
	cur.z=0;
	cur.step=0;
	q.push(cur);
	memset(map,0,sizeof(map));
	map[x][0][0]=1;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		next.step=cur.step+1;
		if(cur.x==0&&cur.y==cur.z||cur.y==0&&cur.x==cur.z||cur.z==0&&cur.x==cur.y)
			return cur.step;
		if(cur.x)
		{
			if(cur.x>y-cur.y)
			{
				next.y=y;
				next.z=cur.z;
				next.x=cur.x-(y-cur.y);
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
			else
			{
				next.y=cur.y+cur.x;
				next.x=0;
				next.z=cur.z;
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
			
			if(cur.x>z-cur.z)
			{
				next.z=z;
				next.x=cur.x-(z-cur.z);
				next.y=cur.y;
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
			else
			{
				next.x=0;
				next.z=cur.z+cur.x;
				next.y=cur.y;
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
		}
		
		if(cur.y)
		{
			next.x=cur.x+cur.y;
			next.y=0;
			next.z=cur.z;
			if(!map[next.x][next.y][next.z])
			{
				q.push(next);
				map[next.x][next.y][next.z]=1;
			}
			
			if(cur.y>z-cur.z)
			{
				next.z=z;
				next.y=cur.y-(z-cur.z);
				next.x=cur.x;
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
			else
			{
				next.y=0;
				next.z=cur.z+cur.y;
				next.x=cur.x;
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
		}
		
		if(cur.z)
		{
			next.x=cur.x+cur.z;
			next.z=0;
			next.y=cur.y;
			if(!map[next.x][next.y][next.z])
			{
				q.push(next);
				map[next.x][next.y][next.z]=1;
			}
			
			if(cur.z>y-cur.y)
			{
				next.y=y;
				next.z=cur.z-(y-cur.y);
				next.x=cur.x;
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
			else
			{
				next.y=cur.y+cur.z;
				next.z=0;
				next.x=cur.x;
				if(!map[next.x][next.y][next.z])
				{
					q.push(next);
					map[next.x][next.y][next.z]=1;
				}
			}
		}
	}
	return -1;
}

int main()
{
	int ans;
	while(scanf("%d%d%d",&x,&y,&z),x||y||z)
	{
		if(x%2!=0)
		{
			printf("NO\n");
			continue;
		}
		ans=BFS();
		if(ans==-1)
			printf("NO\n");
		else 
			printf("%d\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值