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");
        }
    }
}

  

hdu 1010 dfs搜索的更多相关文章

  1. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  2. hdu 1010(DFS) 骨头的诱惑

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...

  3. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  4. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  5. Tempter of the Bone HDU 1010(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  6. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  7. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  8. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. Tempter of the Bone HDU - 1010(dfs)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. jQuery 遍历过滤

    缩写搜索元素的范围 三个最基本的过滤方法是:first(), last() 和 eq(),它们允许您基于其在一组元素中的位置来选择一个特定的元素. 其他过滤方法,比如 filter() 和 not() ...

  2. JS异步阻塞的迷思

    还是百度前端技术学院的“任务十九”可视化排序算法的题,在写出快速排序算法之后,要求用动画的形式把这个排序过程呈现出来.排序过程在CPU里不过是瞬间的事,但要转换成“缓慢的”动画效果给人类看,就不得不把 ...

  3. MySQL 时间戳(Timestamp)函数

    1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp() mysql> select current_timestamp, curren ...

  4. WPF 进度条

    //Create a Delegate that matches the Signature of the ProgressBar's SetValue method private delegate ...

  5. [WinJS] Promise 用法

    初学 WinJS, 可能对 Promise 的用法不大清楚,本文简要说明一下 WinJS中 promise 的基本用法. 主要参考自:http://try.buildwinjs.com/#promis ...

  6. go bytes缓冲区使用介绍

    缓冲区原理简介: go字节缓冲区底层以字节切片做存储,切片存在长度len与容量cap, 缓冲区写从长度len的位置开始写,当len>cap时,会自动扩容.缓冲区读会从内置标记off位置开始读(o ...

  7. [BZOJ 1040] [ZJOI2008] 骑士 【基环+外向树DP】

    题目链接:BZOJ - 1040 题目分析 这道题目的模型就是一个图,不一定联通,每个连通块的点数等于边数. 每个连通块都是一个基环+外向树.即树上增加了一条边. 如果是树,就可以直接树形DP了.然而 ...

  8. prototype.js 源码解读(01)

    prototype.js是一个设计的非常优雅且很有实用价值的js基础类库,其源码非常值得研究.研究它的源码不仅能提升个人水平,而且对你打下坚实的js基础也很有帮助.因本人技术水平有限,该解读仅供参考. ...

  9. Fail2ban用来作DDOS防守工具,不知够不够份量

    http://www.serversyntax.com/2012/12/how-to-secure-centos-server-ssh-fail2ban-ddos-deflate.html http: ...

  10. DLL ActiveForm 线程同步问题

    本文试着从分析Synchronize同步执行的实现机制入手,来解决DLL/ActiveForm中线程同步的问题. 线程中进行同步时调用的Synchronize函数,仅仅是把调用调用线程.调用方法地址. ...