hdu 4740 The Donkey of Gui Zhou dfs 搜索 解题报告

The Donkey of Gui Zhou

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1930    Accepted Submission(s): 646


Problem 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
 

思路: dfs直接暴力。

代码:

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;

int fx[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int n, flag;
int visa[1010][1010], visb[1010][1010];

void dfs(int ax, int ay, int af, int bx, int by, int bf)
{
    if(flag) return ;
    if(ax == bx && ay == by)
    {
        flag = 1;
        printf("%d %d\n", ax, ay);
        return ;
    }
    int f = 0;
    for(int i = af; i < af + 2; i++)
    {
        int tx = ax + fx[i%4][0], ty = ay + fx[i%4][1];

        if(tx >= 0 && ty >= 0 && tx < n && ty < n && !visa[tx][ty])
        {
            f = 1;
            ax = tx;
            ay = ty;
            af = i;
            visa[ax][ay] = 1;
            break;
        }
    }

    for(int i = bf + 4; i > bf + 2; i--)
    {
        int tx = bx + fx[i%4][0], ty = by + fx[i%4][1];
        if(tx >= 0 && ty >= 0 && tx < n && ty < n && !visb[tx][ty])
        {
            f = 1;
            bx = tx;
            by = ty;
            bf = i;
            visb[bx][by] = 1;
            break;
        }
    }

    if(f) dfs(ax, ay, af, bx, by, bf);

}

int main()
{   int ax, ay, af, bx, by, bf;
    while(scanf("%d", &n), n)
    {
        flag = 0;
        memset(visa, 0, sizeof(visa));
        memset(visb, 0, sizeof(visb));
        scanf("%d%d%d", &ax, &ay, &af);
        scanf("%d%d%d", &bx, &by, &bf);
        visa[ax][ay] = 1;
        visb[bx][by] = 1;
        dfs(ax, ay, af, bx, by, bf);
        if(!flag) printf("-1\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值