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. mysql 刘道成视频教程 第3课

    第3课: 1.mysql中条件使用关键字where: 2.查 select name,content from msg ; select name,content 控制列 where id>2 ...

  2. JS中的 this

    JS中的 this 变化多端,似乎难以捉摸,但实际上对 this 的解读,还是有一定规律的. 分析this,该如何下手呢?下面有一个函数 function show(){ alert(this); } ...

  3. 使用 fn 标签 解决字数过多时用省略号代替 .............................

    list列表单条记录某字段大于10就后面添加省略号(如:内容只是显示开始的10个字,后面的用省略号代替) 在list列表中单条记录某字段大于10就后面添加省略号, 首先引入 jstl标签: <% ...

  4. new Image()的用途

    new Image()用途总结: 1.图片预加载      在做游戏时,为了使图片能快打开可以做预加载.      原理:创建image对象,将image对象的src分别指向需加载的图片地址,图片被请 ...

  5. PHP获取当前页面完整url地址,包括参数的函数

    //php获取当前访问的完整url地址 function get_current_url(){     $current_url='http://';     if(isset($_SERVER['H ...

  6. 工欲善其事必先利其器-Notepad++使用小记(Python)

    大学开始就一直使用Notepad++ 作为代码编辑器,喜欢它的简洁明了,喜欢它的个性,也喜欢它各种各样骚气的插件. 今天闲来无事,写篇文章记录一下平时使用的种种,包括但不限于个性化使用一些宏,快捷键, ...

  7. Docker Machine

    Docker Machine http://dockone.io/article/1485?utm_source=tuicool&utm_medium=referral 本地安装与使用 Doc ...

  8. 通过javascript实现1~100内能同时被2和3整除的数并生成如下表格

    请通过javascript实现1~100内能同时被2和3整除的数并生成如下表格: <!DOCTYPE html><html lang="en"><he ...

  9. swift中类似宏定义

    建一个类 如,在Contans.swift中 import UIKit let kMAIN_SIZE = UIScreen.mainScreen().bounds 在其他地方直接用 比如在 MyTab ...

  10. IndexedDB demo showcase

    var dbGlobals = new Object(); dbGlobals.db = null; dbGlobals.description = "This database is us ...