HDU 1010 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; 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
 

//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<30
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;

/**********************************************************************************
题意:
给出一个迷宫地图,‘S’表示起点,‘D’表示终点,‘X’表示墙不能走,
‘.’表示路可以走
限制条件:可以走四个方向,每个路‘.’走过就不能再走,走一格用一秒
要求在T时刻正好到达终点D,时间不能多不能少
思路:
显然不是找最短路了,要找的是长度刚好为T的路径,DFS深搜一下
然后发现深搜超时,看了大牛博客才知道有辣么厉害的剪枝方法
什么路径剪枝奇偶剪枝的,我用的奇偶剪枝
拿出纸笔画个图你会发现,从点(1,1)走到点(3,3)需要的步数永远是偶数步
而从(1,1)走到(3,2)需要步数永远是奇数步,于是我们说
设当前坐标为(x,y),终点坐标为(ox,oy),
若|x-ox| + |y - oy|为偶数,需走偶数步到终点,若为奇数则需奇数步到终点
**********************************************************************************/
int row,cow,T;
char mp[10][10];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int ox,oy;//记录终点
bool dfs(int x,int y,int k)//k表示剩下的步数
{
    if (k == 0) return 0;
    for (int i = 0; i < 4; i++)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx >= 0&&yy >= 0&&xx < row&&yy < cow)
        {
            if (mp[xx][yy] == '.')
            {
                if (((abs(xx-ox)+abs(yy-oy))%2 == 0&&(k-1)%2 == 0)||((abs(xx - ox)+abs(yy-oy))%2 == 1&&(k-1)%2 == 1))
                {
                    mp[xx][yy] = 'X';
                    if (dfs(xx,yy,k-1)) return 1;//搜索有解,直接返回
                    //如果搜索无解,回溯继续
                    mp[xx][yy] = '.';
                }
            }
            if (mp[xx][yy] == 'D')
            {
                if (k == 1) return 1;
            }
        }
    }
    //四个方向走完,没有解
    return 0;//本次搜索无解
}
int main()
{
    while (~scanf("%d%d%d",&row,&cow,&T))
    {
        if (row == 0&&cow == 0&&T == 0) break;
        int x,y;
        for (int i = 0; i < row; i++)
            scanf("%s",mp[i]);
        for (int i = 0; i < row; i ++)
        {
            for (int j = 0; j < cow; j++)
            {
                if (mp[i][j] == 'S')
                {
                    x = i,y = j;
                }
                if (mp[i][j] == 'D')
                {
                    ox = i,oy = j;
                }
            }
        }
        if (((abs(x-ox)+abs(y-oy))%2 == 0&&T%2 == 0)||((abs(x - ox)+abs(y-oy))%2 == 1&&T%2 == 1))
        {
            if (dfs(x,y,T)) puts("YES");
            else puts("NO");
        }
        else puts("NO");
    }
    return 0;
}

你可能感兴趣的:(DFS,杭电)