您的位置:首页 > 产品设计 > UI/UE

HDU 4740 The Donkey of Gui Zhou

2016-05-03 20:26 447 查看
思路:模拟搜索

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int donkey[maxn][maxn];
int tiger[maxn][maxn];
int flag;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int don,tig,n;
void dfs(int a,int b,int c,int x,int y,int z)
{
donkey[a][b]=1;
tiger[x][y]=1;
if (flag)
return;
if (a==x && b==y)   //xiangyu
{
flag=1;
printf("%d %d\n",a,b);
return;
}
if (don && tig)    //dou bu neng zou le
{
flag=1;
printf("-1\n");
return;
}
int dx,dy,da,db;
if (don)
{
da= a;
db = b;
}
else
{
da = a+dir[c][0];
db = b+dir[c][1];
if (da<0 || db<0 || da>=n || db>=n || donkey[da][db]==1)
{
c = (c+1)%4;
da = a+dir[c][0];
db = b+dir[c][1];
if (da<0 || db<0 || da>=n || db>=n || donkey[da][db]==1)
{
don=1;
da=a;
db=b;
}
}
}
if (tig)
{
dx = x;
dy = y;
}
else
{
dx = x+dir[z][0];
dy = y+dir[z][1];
if (dx<0 || dy<0 || dx>=n || dy>=n || tiger[dx][dy]==1)
{
z = (z+3)%4;
dx = x+dir[z][0];
dy = y+dir[z][1];
if (dx<0 || dy<0 || dx>=n || dy>=n || tiger[dx][dy]==1)
{
tig = 1;
dx = x;
dy = y;
}
}
}
dfs(da,db,c,dx,dy,z);
}
int main()
{
while (scanf("%d",&n)!=EOF && n)
{
memset(donkey,0,sizeof(donkey));
memset(tiger,0,sizeof(tiger));
tig=0;
don=0;
flag=0;
int a,b,c,x,y,z;
scanf("%d%d%d",&a,&b,&c);
scanf("%d%d%d",&x,&y,&z);
dfs(a,b,c,x,y,z);
}
}


Description

There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be considered as an N×N grid. The coordinates of the up-left cell is (0,0) , the down-right cell is (N-1,N-1)
and the cell below the up-left cell is (1,0)..... A 4×4 grid is shown below:



The donkey lived happily until it saw a tiger far away. The donkey had never seen a tiger ,and the tiger had never seen a donkey. Both of them were frightened and wanted to escape from each other. So they started running fast. Because they were scared, they
were running in a way that didn't make any sense. Each step they moved to the next cell in their running direction, but they couldn't get out of the forest. And because they both wanted to go to new places, the donkey would never stepped into a cell which
had already been visited by itself, and the tiger acted the same way. Both the donkey and the tiger ran in a random direction at the beginning and they always had the same speed. They would not change their directions until they couldn't run straight ahead
any more. If they couldn't go ahead any more ,they changed their directions immediately. When changing direction, the donkey always turned right and the tiger always turned left. If they made a turn and still couldn't go ahead, they would stop running and
stayed where they were, without trying to make another turn. Now given their starting positions and directions, please count whether they would meet in a cell.

Input

There are several test cases.

In each test case:

First line is an integer N, meaning that the forest is a N×N grid.

The second line contains three integers R, C and D, meaning that the donkey is in the cell (R,C) when they started running, and it's original direction is D. D can be 0, 1, 2 or 3. 0 means east, 1 means south , 2 means west, and 3 means north.

The third line has the same format and meaning as the second line, but it is for the tiger.

The input ends with N = 0. ( 2 <= N <= 1000, 0 <= R, C < N)

Output

For each test case, if the donkey and the tiger would meet in a cell, print the coordinate of the cell where they meet first time. If they would never meet, print -1 instead.

Sample Input

2
0 0 0
0 1 2
4
0 1 0
3 2 0
0


Sample Output

-1
1 3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: