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

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

题目描述

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 109915 Accepted Submission(s): 29877

Problem 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

题目理解

​ doggie起始位置为S,’X’标记为墙,’.’标记为可以行走的路,’D’则是出口。在开始时,从输入给出时间T,要寻找这样一条路,使得doggie在第T秒到达’D’位置,若存在则视为doggie存活,反之视为死亡。

​ 注意,S并不一定是迷宫的(0, 0)位置,且不是在T时间内到达’D’,而是在T时间刚好到达’D’。

​ 寻路过程中,访问过的路不能重复访问。

简单DFS

​ 有了上面的理解,很容易就可以构造出简单的DFS结构去处理这个问题。

void dfs(int x, int y, int time)
{
    if (flag) {
        return ;
    }
    else if (x == ex && y == ey && time == 0) {  //ex、ey记录'D'位置
        flag = 1;
        return ;
    }

    int i;
    for (i = 0; i < 4; ++i) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];

        if (tx >= 0 && tx < n && ty >= 0 && ty < m) {  //位置的判断,不能数组越界
            if (maze[tx][ty] != 'X' && !visit[tx][ty]) {  //访问过的路不能再走
                visit[tx][ty] = 1;  //回溯的过程
                dfs(tx, ty, time - 1);  //不能time++或者++time,否则会造成回溯时time被改变
                visit[tx][ty] = 0;
            }
        }
    }
}

​ 这样写能得到正确结果,但提交之后是会超时的。所以需要做一些剪枝处理。下面就来说一下用到的剪枝手段。

奇偶剪枝

​ 题目有一个前提条件,doggie只能以上下左右4个方向移动,不能以对角线方向移动,所以在这里就可以使用到奇偶剪枝了。对于奇偶剪枝,先来了解一下基本用法:

假定有一个矩阵A(如下图):

奇偶剪枝示意图

由图可知,矩阵任意元素相邻元素都与其不相同,仅移动1步,到达的位置的元素必定是与当前元素不同的元素,举个例子,任意位置上的0,仅移动1步,所到达的所有可能位置都是1,对于任意位置上的1同理。因此,若要从0到0,或者从1到1,必定至少需要偶数步。

​ 将奇偶剪枝应用到这题中,还需要另一个概念,曼哈顿距离。

​ 曼哈顿距离计算的是绝对轴距和,公式定义如下:

|x1 - x2| + |y1 - y2|

​ 这里我也用一个图来说明一下为什么曼哈顿距离在这里使用是有效的:

曼哈顿距离示意图

​ 图中表示的红、蓝和黄线的曼哈顿距离是相等的。

​ 基于曼哈顿距离与奇偶剪枝,在代码中可以做处理的部分就有2处:

  1. 起始状态时,即dfs执行之前先判断D是否不可达,对于不可达的情况则不需要执行dfs寻路了,具体操作是计算曼哈顿距离dis,将其与给定时间t求和,并对2求模,代码如下:
        int dis = abs(ex - sx) + abs(ey - sy);
        if ((dis + t) % 2 != 0) {  //若可达,则dis与t应该是同奇或者同偶的,因此和必定为偶数
            printf("NO\n");
            continue;
        }
  1. dfs过程中,动态计算当前位置与D的曼哈顿距离,并判断是否大于移动到当前位置所剩时间time,代码如下:
        int dis = abs(ex - x) + abs(ey - y);
        if (dis > time) {  //剩余时间不足,应该剪枝
            return ;
        }

其他一些剪枝

​ 还有一些剪枝的部分,比如判断当前位置时间是否耗尽之类的,就不说了,比较基本的判断。

完整代码实现

​ 对于1010这题,关键点也在代码中注释了,下面给出完整代码实现:

//
//  main.cpp
//  hdu1010
//
//  Created by Morris on 16/7/21.
//  Copyright © 2016年 Morris. All rights reserved.
//

#include <cstdio> 
#include <cstring> 
#include <cstdlib>

#define MAX_SZ 7

namespace {
    using std::scanf;
    using std::printf;
    using std::memset;
    using std::abs;
}

int flag = 0;
int n, m, t;
int sx = 0, sy = 0;
int ex = 0, ey = 0;
char maze[MAX_SZ][MAX_SZ] = { 0 };
int visit[MAX_SZ][MAX_SZ] = { 0 };
/* up, right, down, left */
int dir[4][2] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } };

void dfs(int x, int y, int time);

int main(int argc, const char *argv[])
{
    int i, j;
    while (~scanf("%d%d%d", &n, &m, &t)) {
        if (n == 0 && m == 0 && t == 0) {
            break;
        }

        for (i = 0; i < n; ++i) {
            scanf("%s", maze[i]);
        }

        for (i = 0; i < n; ++i) {
            for (j = 0; j < m; ++j) {
                if (maze[i][j] == 'S') {
                    sx = i;
                    sy = j;
                }
                if (maze[i][j] == 'D') {
                    ex = i;
                    ey = j;
                }
            }
        }

        int dis = abs(ex - sx) + abs(ey - sy);
        if ((dis + t) % 2 != 0) {  //可达性判断
            printf("NO\n");
            continue;
        }

        memset(visit, 0, sizeof(visit));
        flag = 0;
        visit[sx][sy] = 1;
        dfs(sx, sy, t);
        if (flag == 1) {
            printf("YES\n");
        }
        else {
            printf("NO\n");
        }
    }

    return 0;
}

void dfs(int x, int y, int time)
{
    if (flag) {
        return ;
    }
    else if (time < 0) {  //时间耗尽,并未到达D
        return ;
    }
    else if (x == ex && y == ey && time == 0) {  //恰好到达D
        flag = 1;
        return ;
    }

    int dis = abs(ex - x) + abs(ey - y);
    if (dis > time) {  //剩余时间不足
        return ;
    }

    int i;
    for (i = 0; i < 4; ++i) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];

        if (tx >= 0 && tx < n && ty >= 0 && ty < m) {  //位置判断,数组不能越界
            if (maze[tx][ty] != 'X' && !visit[tx][ty]) {  //不能移动到走过的路
                visit[tx][ty] = 1;  //回溯过程
                dfs(tx, ty, time - 1);  //不能time++或者++time,否则会造成回溯时time被改变
                visit[tx][ty] = 0;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值