DFS - Parity Pruning

Pruning needed in search 

1. The number of steps step > T in the current number of steps requires pruning;

2. Assume that the shortest distance from the current point to the end point is s, and the current number of steps is step. If the specified number of steps is T - step < s, pruning is required;

3. At the current position, (T - step - s) is an odd number and needs to be pruned;

4. In the map, when the number of points that can be taken is less than the specified time or number of steps T, pruning is required.

Topic link: Click to open the link

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


    It is easier to learn if you feel that you have learned it and understand it.

    

#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

char map[10][10];
int n, m,  t ,flag ,vis[10][10];
int num, sx, sy, dx, dy;
int dx1[4] = {-1,1,0,0}, dy1[4] = {0,0,-1,1};

void Dfs (int x, int y , int z){
    int x1,y1;
    if (flag == 1){
        return ;
    }
    if (x == dx && y == dy && z == t){
        flag = 1;
        return ;
    }
    if (z > t){ //The number of steps taken at the current number of steps step > T, need to be pruned;
        return ;
    }
    int minj = abs(x - dx) + abs(y - dy);
    if (minj > t - z || (t - minj - z) % 2 == 1){ // . Assume that the shortest distance from the current point to the end point is s, and the current number of steps is step, if the specified number of steps T - step < s, pruning is required || At the current position, (T - step - s) is an odd number, and pruning is required;
        return ;
    }
    for (int i = 0; i < 4; i++){
        x1 = x + dx1[i];
        y1 = y + dy1[i];
        if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && !vis[x1][y1] && map[x1][y1] != 'X'){
            vis [x1] [y1] = 1;
            Dfs(x1,y1,z + 1);
            vis [x1] [y1] = 0;
        }
    }
}

int main(){
    while (~scanf("%d%d%d",&n,&m,&t) && (n && m && t)){
        num = 0 ;
        for (int i = 0; i < n; i++){
            scanf("%s",map[i]);
            for (int j = 0 ; j < m; j++){
                if (map[i][j] == 'S'){
                    sx = i; sy = j;
                }
                if (map[i][j] == 'D'){
                    dx = i; dy = j ;
                }
                if (map[i][j] == 'X'){
                    num++ ;
                }
            }
        }
        if (n * m - num - 1 < t){ // In the map, when the number of points that can be moved is less than the specified time or number of steps T, pruning is required.
            printf("NO\n");
            continue ;
        }
        flag = 0;
        memset(vis,0, sizeof (vis));
        vis [sx] [sy] = 1;
        Dfs (sx, sy, 0);
        if (flag){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325885075&siteId=291194637