hdu 1010 dfs搜索

时间:2022-06-28 05:09:11

Tempter of the Bone

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

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

题目大意是要在那个时间点找到D点,典型的DFS问题,重点是要求奇偶减枝

#include <stdio.h>
#include <math.h>
#include <cmath>
#include <iostream>
using namespace std;
#define Maxn 100

int hang,lie,Time;
char MAP[Maxn][Maxn];
int begin_x,begin_y;
int end_x,end_y;
bool flag;
int dir[4][2] = {
    {0,1},
    {0,-1},
    {1,0},
    {-1,0}
};

void print()
{
    for(int i = 0; i < hang; i++)
    {
        for(int j = 0; j < lie; j++)
        {
            printf("%c",MAP[i][j]);
        }
        printf("\n");
    }
}

void dfs(int x, int y, int t)
{
    // print();
    // printf("\n");
    if (flag)
    {
        return ;
    }
    // printf("Q\n", );
    if (x == end_x && y == end_y && t == Time)
    {
        // printf("YES~~~~~~~~~~~\n");
        flag = true;
        return ;
    }
    int temp = (Time - t) - ( abs(x- end_x) + abs(y - end_y) );
    if (temp < 0 || temp & 1)
    {
        return ; // 奇偶剪枝
        /*要理解奇偶剪枝,先了解一下曼哈顿距离,
        从一个点到达另外一个点的最
        短路径长度(时间)可以根据两点坐标求出,
        路径长度(非最短)与最短路径的长度同奇偶,
        它们的差一定是偶数!举个例子,就像两个偶数的差
        差是偶数,两个个数的差也是偶数.*/
    }
    for(int i = 0; i < 4; i++)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if (xx >= 0 && xx < hang && yy >= 0 && yy < lie)
        {
            if (MAP[xx][yy] != 'X')
            {
                MAP[xx][yy] = 'X';
                dfs(xx,yy,t+1);
                MAP[xx][yy] = '.';
            }
        }
    }
    return ;
}

int main()
{
    while(cin >> hang >> lie >> Time,hang + lie + Time)
    {
        flag = false;
        for(int i = 0; i < hang; i++)
        {
            scanf("%s",MAP[i]);
        }
        for(int i = 0; i < hang; i++)
        {
            for(int j = 0; j < lie; j++)
            {
                if (MAP[i][j] == 'S')
                {
                    begin_x = i;
                    begin_y = j;
                }
                else if (MAP[i][j] == 'D')
                {
                    end_x = i;
                    end_y = j;
                }
            }
        }
        // printf("%d %d\n",begin_x,begin_y);
        MAP[begin_x][begin_y] = 'X';
        dfs(begin_x,begin_y,0);
        if (flag)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
}