hdu 4740—€—The Donkey of Gui Zhou

bfs


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct Node
{
	int dx,dy,dd;
	int tx,ty,td;
}tmp;
int n;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int tvis[1100][1100],dvis[1100][1100];
void next(Node &u)
{
	int x,y;
	x=u.dx+dir[u.dd][0];
	y=u.dy+dir[u.dd][1];
	if(x>=0&&x<n&&y>=0&&y<n&&!dvis[x][y])
	{
		u.dx=x;
		u.dy=y;
	}
	else
	{
		x=u.dx+dir[(u.dd+1)%4][0];
		y=u.dy+dir[(u.dd+1)%4][1];
		if(x>=0&&x<n&&y>=0&&y<n&&!dvis[x][y])
		{
			u.dx=x;
			u.dy=y;
			u.dd=(u.dd+1)%4;
		}
	}
	x=u.tx+dir[u.td][0];
	y=u.ty+dir[u.td][1];
	if(x>=0&&x<n&&y>=0&&y<n&&!tvis[x][y])
	{
		u.tx=x;
		u.ty=y;
	}
	else
	{
		x=u.tx+dir[(u.td-1+4)%4][0];
		y=u.ty+dir[(u.td-1+4)%4][1];
		if(x>=0&&x<n&&y>=0&&y<n&&!tvis[x][y])
		{
			u.tx=x;
			u.ty=y;
			u.td=(u.td-1+4)%4;
		}
	}
}
int bfs(Node u)
{
	int dprex=-1,dprey=-1;
	int tprex=-1,tprey=-1;
	queue<Node> q;
	dvis[u.dx][u.dy]=1;
	tvis[u.tx][u.ty]=1;
	q.push(u);
	while(!q.empty())
	{
		u=q.front();
		//printf("\n%d %d\n%d %d\n\n",u.dx,u.dy,u.tx,u.ty);
		q.pop();
		next(u);
		//printf("\n%d %d\n%d %d\n\n",u.dx,u.dy,u.tx,u.ty);
		if(u.dx==u.tx&&u.dy==u.ty)
		{
			tmp=u;
			return 1;
		}
		else
		{
			if(dprex==u.dx&&dprey==u.dy&&tprex==u.tx&&tprey==u.ty)
				return 0;
			dvis[u.dx][u.dy]=1;
			tvis[u.tx][u.ty]=1;
			dprex=u.dx;dprey=u.dy;
			tprex=u.tx;tprey=u.ty;
			q.push(u);
		}
	}
	return 0;
}
int main()
{
	while(scanf("%d",&n)&&n)
	{	
		memset(dvis,0,sizeof(dvis));
		memset(tvis,0,sizeof(tvis));
		scanf("%d%d%d",&tmp.dx,&tmp.dy,&tmp.dd);
		scanf("%d%d%d",&tmp.tx,&tmp.ty,&tmp.td);
		if(tmp.dx==tmp.tx&&tmp.dy==tmp.ty)	
		{
			printf("%d %d\n",tmp.dx,tmp.dy);
			continue;
		}
		if(bfs(tmp)==0)
			printf("-1\n");
		else
			printf("%d %d\n",tmp.dx,tmp.dy);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值