hdu 1495

好久没有搜索一A的感觉了,爽

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

int vis[102][102][102];
int s,n,m;
struct point
{
	int x;
	int y;
	int z;
	int step;
};
queue<point> q;

int bfs(int x,int y,int z)
{
	point t,hd;
	while(!q.empty())
		q.pop();
	t.x=x;
	t.y=y;
	t.z=z;
	t.step=0;
	vis[x][y][z]=1;
	q.push(t);
	while(!q.empty())
	{
		hd=q.front();
		q.pop();
		if((hd.x==hd.y&&hd.z==0)||(hd.y==hd.z&&hd.x==0)||(hd.x==hd.z&&hd.y==0))
	{
		return hd.step;
	}
		else
		{
			t.step=hd.step+1;
			if(hd.x+hd.y>n)
			{
				t.x=hd.x-(n-hd.y);	
				t.y=n;
				t.z=hd.z;
			}
			else
			{
				t.x=0;
				t.y=hd.x+hd.y;
				t.z=hd.z;
			}
			if(vis[t.x][t.y][t.z]==0)
			{
				vis[t.x][t.y][t.z]=1;
				q.push(t);
			}
			if(hd.x+hd.z>m)
			{
				t.x=hd.x-(m-hd.z);
				t.y=hd.y;
				t.z=m;
			}
			else
			{
				t.x=0;
				t.y=hd.y;
				t.z=hd.x+hd.z;
			}
			if(vis[t.x][t.y][t.z]==0)
			{
				vis[t.x][t.y][t.z]=1;
				q.push(t);
			}
			if(hd.y+hd.x>s)
			{
				t.x=s;
				t.y=hd.y-(s-hd.x);
				t.z=hd.z;
			}
			else
			{
				t.x=hd.y+hd.x;
				t.y=0;
				t.z=hd.z;
			}
			if(vis[t.x][t.y][t.z]==0)
			{
				vis[t.x][t.y][t.z]=1;
				q.push(t);
			}
			if(hd.y+hd.z>m)
			{
				t.x=hd.x;
				t.y=hd.y-(m-hd.z);
				t.z=m;
			}
			else
			{
				t.x=hd.x;
				t.y=0;
				t.z=hd.y+hd.z;
			}
			if(vis[t.x][t.y][t.z]==0)
			{
				vis[t.x][t.y][t.z]=1;
				q.push(t);
			}
			if(hd.z+hd.x>s)
			{
				t.x=s;
				t.y=hd.y;
				t.z=m-(s-hd.x);
			}
			else
			{
				t.x=hd.z+hd.x;
				t.y=hd.y;
				t.z=0;
			}
			if(vis[t.x][t.y][t.z]==0)
			{
				vis[t.x][t.y][t.z]=1;
				q.push(t);
			}
			if(hd.z+hd.y>n)
			{
				t.x=hd.x;
				t.y=n;
				t.z=hd.z-(n-hd.y);
			}
			else
			{
				t.x=hd.x;
				t.y=hd.y+hd.z;
				t.z=0;
			}
			if(vis[t.x][t.y][t.z]==0)
			{
				vis[t.x][t.y][t.z]=1;
				q.push(t);
			}
		}
	}
	return 0;
}
int main()
{
	while(cin>>s>>n>>m)
	{
		memset(vis,0,sizeof(vis));
		if(s+n+m==0)
			break;
		if(s%2)
		{
			cout<<"NO"<<endl;
			continue;
		}
		vis[s][0][0]=1;
		int ans=0;
		if((ans= bfs(s,0,0)))
			cout<<ans<<endl;
		else
			cout<<"NO"<<endl;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值