hud 4740 The Donkey of Gui Zhou(深搜DFS)

The Donkey of Gui Zhou
Time Limit:1000MS     Memory Limit:32768KB    
 64bit IO Format:%I64d & %I64u

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 he 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
0
4
0 1
3 2 0
0

Sample Output

         
         
-1
1 3
 
题意:有一个N*N的方格,a donkey和a tiger分别在一个小格子里面,donkey只能往前或
往右走,tiger只能往前或  往左走。同样地,当不能转弯时,就停下来。他们没走一格,
所花的时间一样。输出他们第一次相遇的地点, 即格子的坐标。

代码:
#include <cstdio>
#include <cstring>
using namespace std;

#define MAXN 1005

int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};//东南西北
int n,p,q;
int donkey[MAXN][MAXN],tiger[MAXN][MAXN];

void DFS(int a,int b,int c,int x,int y,int z)
{
    donkey[a][b]=1;
    tiger[x][y]=1;
    if(a==x&&b==y)
    {
        printf("%d %d\n",a,b);
        return ;
    }
    if(p&&q)
    {
        printf("-1\n");
        return ;
    }
    int aa,bb,xx,yy;
    if(p)
    {
        aa=a;
        bb=b;
    }
    else
    {
        aa=a+dir[c][0];
        bb=b+dir[c][1];
        if(aa<0||bb<0||aa>=n||bb>=n||donkey[aa][bb]==1)
        {
            c=(c+1)%4;//向右
            aa=a+dir[c][0];
            bb=b+dir[c][1];
            if(aa<0||bb<0||aa>=n||bb>=n||donkey[aa][bb]==1)
            {
                p=1;
                aa=a;
                bb=b;
            }
        }
    }
    if(q)
    {
        xx=x;
        yy=y;
    }
    else
    {
        xx=x+dir[z][0];
        yy=y+dir[z][1];
        if(xx<0||yy<0||xx>=n||yy>=n||tiger[xx][yy]==1)
        {
            z=(z-1+4)%4;//向左
            xx=x+dir[z][0];
            yy=y+dir[z][1];
            if(xx<0||yy<0||xx>=n||yy>=n||tiger[xx][yy]==1)
            {
                q=1;
                xx=x;
                yy=y;
            }
        }
    }
    DFS(aa,bb,c,xx,yy,z);
}

int main()
{
    int a,b,c,x,y,z;
    while(scanf("%d",&n),n)
    {
        scanf("%d%d%d",&a,&b,&c);
        scanf("%d%d%d",&x,&y,&z);
        if(a==x&&b==y)
        {
            printf("%d %d\n",a,b);
            continue;
        }
        else
        {
            memset(donkey,0,sizeof(donkey));
            memset(tiger,0,sizeof(tiger));
            p=q=0;
            DFS(a,b,c,x,y,z);
        }
    }
    return 0;
}
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值