首页 > 搜索 > DFS搜索 > HDU 4740-The Donkey of Gui Zhou-DFS-[解题报告]HOJ
2015
09-17

HDU 4740-The Donkey of Gui Zhou-DFS-[解题报告]HOJ

The Donkey of Gui Zhou

问题描述 :

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:
Zhuge Liang's Mines

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.

输入:

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)

输出:

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)

样例输入:

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

样例输出:

-1
1 3

深搜

题意:

在一个N*N的方格里,有一只驴和一只虎,两者以相同的速度(一格一格地走)同时开始走。走法是:往东南西北某一个初始方向走,两者都不能重复走自己走过的路,但是对方走过的路自己可以走,如果遇到墙壁或者自己走过的路,则驴向右转,虎向左转,如果还不能继续往前走,就停在原地不动。如果驴和老虎能同一时间在一个格子里面相遇,则输出坐标,否则输出-1 。

分析:

深搜。

1)当两者坐标相同时,相遇;

2)两者同时在不同地方都停下时,肯定不相遇;

3)如果有一个停下了,记录当前坐标,否则往初始方向继续遍历。

AC代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;

#define MAXN 1005

int dir[4][2] = { {0,1}, {1,0}, {0,-1}, {-1,0} } ;          //东、南、西、北
int n, flag, 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(flag) return ;
    if(a == x && b == y) {
        flag = 1;
        printf("%d %d\n", a, b);
        return ;
    }
    if(p && q) {
        flag = 1;
        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 ));
            flag = p = q = 0;
            DFS(a, b, c, x, y, z);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/chuck_0430/article/details/11747831


, ,