HDU1010 Tempter of the Bone 深搜DFS-题解

● 本题解会有详细的分析,适合初学者阅读

原题

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;
‘.’: 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

题目翻译

Problem Description

迷宫是一个大小为N×M的矩形。迷宫里有一扇门。一开始,门是关着的,它会在第T秒打开一小段时间(不到1秒)。因此,小狗必须正好在第T秒到达门口。在每一秒钟里,他都可以移动到上、下、左、右相邻区域。一旦他进入一个区域,这个区域的地面就会开始下沉,并在下一秒消失。他不能在一个区域停留超过一秒钟,也不能进入一个已经访问过的区域。可怜的小狗能活下来吗?请帮帮他。

Input

输入由多个测试用例组成。每个测试用例的第一行包含三个整数N、M和T(1<N、M<7;0<T<50),分别表示迷宫的大小和门打开的时间。接下来的N行给出了迷宫布局,每行包含M个字符。字符是下列字符之一:

“X”:一堵墙,小狗不能进去;

“S”:狗的起点;

“D”:门;

“.”:空区域。

Output

对于每组测试数据,如果狗能活下来输出YES,活不下来输出NO。

题目分析

走迷宫问题,确定的这题是一道DFS深搜的练习题,且为普通DFS联通性模型,

DFS的模板框架:

function dfs(当前状态){
    
    
	if(当前状态 == 目的状态){
    
    
        ···
    }
    for(···寻找新状态){
    
    
        if(状态合法){
    
    
            vis[访问该点]dfs(新状态);
            ?是否需要恢复现场->vis[恢复访问]
        } 
    }
    if(找不到新状态){
    
    
        ···
    }
}

首先分析搜索的思路:在限定条件下,测试两点的连通性,我们先考虑状态的表示和初态、末态以及搜索的成功和失败

状态的表示:题目已经提供了初态和理想模态-即起点和终点,同时,我们需要一个状态指示变量,表示当前走的步数(就是原题里的秒数)

搜索的成功:在给定的T步正好搜到终点,即为搜索成功

**搜索的失败:**无法搜到终点/步数超出T步/步数未达到T步就到达

接下来分析如何寻找新状态:小狗只能向上下左右四个方向移动,打一个坐标变化量数组然后遍历数组对坐标相加即可。

因为一次可能搜不到合法路径,因此需要回溯!

在访问时,我们对已经访问过的元素进行标记,在访问结束后需要“恢复现场”,以便下一次搜索。

搜素的部分分析完了显然没分析完,这么简单就能AC吗?看看时间限制,显然我们需要对搜素进行剪枝,避免一些无意义的搜索造成的时间开销

如何剪枝?

n ∗ m − w a l l < = t n*m-wall<=t nmwall<=t. n ∗ m n*m nm代表总格数, n ∗ m − w a l l n*m-wall nmwall代表能走的格子数目, t t t是时间, n ∗ m − w a l l < = t n*m-wall<=t nmwall<=t也就是可以走的格子走完了时间T都没有到,显然没必要继续搜索下去,一定不符合题意。

②可行性剪枝+奇偶性剪枝:这是个技术活,写的时候考虑不到没关系,交一次如果发现TLE就应该考虑一下了

这里依据的原理是:两点之间直线最短,所以由S到达D的最短路径就是 a b s ( s x − d x ) + a b s ( s y − d y ) abs(sx-dx)+abs(sy-dy) abs(sxdx)+abs(sydy), t − s t e p t-step tstep就代表你任意走的总步数,因为 a b s ( s x − d x ) + a b s ( s y − d y ) abs(sx-dx)+abs(sy-dy) abs(sxdx)+abs(sydy)已经是最短的步数了(直角坐标系下),不可能再存在方案可能比它更短。

所以当 t − s t e p < a b s ( s x − d x ) + a b s ( s y − d y ) t-step<abs(sx-dx)+abs(sy-dy) tstep<abs(sxdx)+abs(sydy)的时候,就应该判定为不合法将函数出栈了。

此外: t − s t e p − a b s ( s x − d x ) − a b s ( s y − d y ) t-step-abs(sx-dx)-abs(sy-dy) tstepabs(sxdx)abs(sydy)必须是偶数,如果是奇数就不能到达D。如何证明?

示例图片

证明: t − s t e p t-step tstep为偶,则在t步恰好到达终点

第t步到达终点那么有 t = s t e p + e x t r a s t e p t = step + extra_step t=step+extrastep,移项得:整理得 t − s t e p p = e x t r a s t e p t - stepp = extra_step tstepp=extrastep.

要证 t − p t - p tp 为偶数, 即证 e x t r a extra extra为偶数:

从起点出发,假设图中黄色路径为唯一最短路(演示方便),将其余步骤类比展开(起点终点不计入步数),如图所示

展开图

显然,所有绕开最短路走的情况可以视为多进行了两次转向,也就是说,只有经历偶数次转向,才能够到达终点,因此可以证明 e x t r a extra extra就是额外的转向,必为偶数。因此题设得证(可举出若干示例辅证)

AC Code

#include <bits/stdc++.h>
#define xa x + a[i] //x方向进行移动
#define yb y + b[i] //y方向进行移动
using namespace std;
char g[8][8];
int a[] = {
    
    0, 0, 1, -1};		//配合xa食用
int b[] = {
    
    -1, 1, 0, 0};		//配合yb食用
int n, m, t, step, remain, destx, desty;		//destx,dexty记录的为终点坐标
bool flag;		//搜索成功标记
void dfs(int x, int y, int tc)
{
    
    
    if (tc == t && x == destx && y == desty){
    
    		//检查是否搜索成功
        flag = true;
        return;
    }
    //可行性剪枝+奇偶性剪枝
    int v = t - tc - abs(destx - x) - abs(desty - y); 
    if ((abs(destx - x) + abs(desty - y)) % 2 != (t - tc) % 2)  return;                
    for (int i = 0; i < 4; i++){
    
    
        if (xa >= 0 && xa < n && yb >= 0 && yb < m && g[xa][yb] != 'X'){
    
    
            g[xa][yb] = 'X';
            dfs(xa, yb, tc + 1); 
            g[xa][yb] = '.';  
        }
    }
}
int main(){
    
    
    while (cin >> n >> m >> t){
    
    
        if (n == 0 && m == 0 && t == 0) break;
        step = remain = 0;
        flag = false;
        int icur, jcur;
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
            {
    
    
                cin >> g[i][j];
                if (g[i][j] == 'S') icur = i, jcur = j;
                else if (g[i][j] == 'D') remain++, destx = i, desty = j;
                else if (g[i][j] == '.') remain++;
            }
        g[icur][jcur] = 'X';
        if (remain >= t)  dfs(icur, jcur, 0);
        if (flag) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yanweiqi1754989931/article/details/113061790