码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1010 && ZOJ 2110 Tempter of the bone (DFS + 奇偶剪枝)

时间:2015-03-02 19:07:09      阅读:178      评论:0      收藏:0      [点我收藏+]

标签: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


详细代码及解析如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#define MAXN 100
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

char Map[MAXN][MAXN];      //地图
int N, M, T;            //地图size,逃脱时间
bool escape = false;        //能否逃脱
int Sx, Sy, Ex, Ey, wall;     //起始坐标,目标坐标,墙壁数目
const int dx[] = {0, 0, 1, -1};    //方向数组
const int dy[] = {-1, 1, 0, 0};

bool check(int x, int y)   //判断是否能往下一个格子前进
{
    return x>=0 && y>=0 && x<N && y<M && Map[x][y]!='X';
}

void DFS(int px, int py, int cnt)
{
    if(px==Ex && py==Ey && cnt==T) {   //顺利逃脱
        escape = true;
        return ;
    }
    //abs(x-ex)+abs(y-ey)表示现在所在的格子到目标格子的距离(不能走对角线)
    //T-cnt是实际还需要的步数,将它们做差
    //如果tmp<0或tmp为奇数,那就不可到达!
    int tmp = (T-cnt) - fabs(px-Ex) - fabs(py-Ey);   //搜索过程中的奇偶剪枝
    if(tmp < 0 || tmp % 2) return ;
    for(int i=0; i<4; i++) {
        int xx = px + dx[i];
        int yy = py + dy[i];
        if(check(xx, yy)) {
            Map[xx][yy] = 'X';      //将当前方格设置为墙壁
            DFS(xx, yy, cnt+1);     //深度遍历
            if(escape) return ;    
            Map[xx][yy] = '.';     //状态恢复,回溯
        }
    }
    return ;
}

int main(int argc, char *argv[])
{
    while(~scanf("%d %d %d", &N, &M, &T) && N || M || T) {
        wall = 0;
        for(int i=0; i<N; i++) {    //地图输入
            scanf("%s", Map[i]);
            for(int j=0; Map[i][j]; j++) {
                if(Map[i][j] == 'S') { Sx=i, Sy=j; }
                else if(Map[i][j] == 'D') { Ex=i, Ey=j; }
                else if(Map[i][j] == 'X') wall++;   //统计墙的块数
            }
        }
        if(N*M-wall <= T) {    //搜索前的剪枝
            puts("NO");
            continue;
        }
        escape = false;        //初始化为false
        Map[Sx][Sy] = 'X';     //走过的块变成墙,保证一次走过
        DFS(Sx, Sy, 0);
        if(escape) puts("YES");
        else puts("NO");
    }
    return 0;
}


HDU 1010 && ZOJ 2110 Tempter of the bone (DFS + 奇偶剪枝)

标签:dfs   奇偶剪枝   

原文地址:http://blog.csdn.net/keshacookie/article/details/44021213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!