HDU 1010 Tempter of the Bone (DFS + 奇偶剪枝)

来源:互联网 发布:淘宝装修教程新手入门 编辑:程序博客网 时间:2024/06/06 12:30

Description

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X’: a block of wall, which the doggie cannot enter;
‘S’: the start point of the doggie;
‘D’: the Door; or
‘.’: an empty block.

The input is terminated with three 0’s. This test case is not to be processed.

Output

For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.

Sample Input

4 4 5
S.X.
..X.
..XD
….
3 4 5
S.X.
..X.
…D
0 0 0

Sample Output

NO
YES

题解:

题目问能不能刚好在第T步走到门口。

刚拿到题目有点懵,做惯了求最短步数的,而此题步数小于T还不行,要刚刚好等于T,真真是有毛病~

仔细想想,不难找到解决方案,设置个剩余步数的变量leave,当leave == 0是才判断是否在门口。

另外,此题还用到了:

奇偶剪枝

这里简单介绍结论:
判断能不能恰好在第t步是走出迷宫,公式是:
t-[abs(ex-sx)+abs(ey-sy)],若其结果为非偶数(奇数),则无法在t步恰好到达;
ex, ey是终点坐标。当然有些同学会每步都进行就剪枝,其实没必要,只要开始时判断就行了

#include <iostream>#include <cmath>using namespace std;const int MAXN = 10;int r, c, t, sy, sx, ey, ex;int direct[4][2] = { {0,-1}, {0,1}, {-1,0}, {1,0} };char pic[MAXN][MAXN];bool escape;void dfs(int y, int x, int leave) {    if (y == ey && x == ex && leave == 0) {        escape = true;         return;    }    for (int i = 0; i < 4; ++i) {        int nexty = y + direct[i][0];        int nextx = x + direct[i][1];        if (nexty < 0 || nexty >= r || nextx < 0 || nextx >= c || pic[nexty][nextx] != '.') {            continue;        }        if (escape) return;        pic[nexty][nextx] = 'X';        dfs(nexty, nextx, leave - 1);        pic[nexty][nextx] = '.';    }    return;}int main() {    while (scanf("%d%d%d%*c", &r, &c, &t) == 3 && r && c && t) {        for (int i = 0; i < r; ++i) {            for (int j = 0; j < c; ++j) {                pic[i][j] = getchar();                if (pic[i][j] == 'S') { sy = i; sx = j; }                if (pic[i][j] == 'D') { ey = i; ex = j; }            }            getchar();        }        escape = false;        pic[ey][ex] = '.';        int temp = t - abs(sy - ey) - abs(sx - ex);        if (temp >= 0 && !(temp & 1)) dfs(sy, sx, t);        printf("%s\n", escape ? "YES" : "NO");    }    return 0;}
0 0
原创粉丝点击