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. Java 设计模式_代理模式(2016-08-19)

    概念: 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 就是一个人或者机构代表另一个人或者机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一 ...

  2. storm简介[ZZ]

    场景 伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样.更加便捷,同时对于信息的时效性要求也越来越高.举个搜索 场景中的例子,当一个卖家发布了一条宝贝信息时,他希望的 ...

  3. 分享一个自己写的基于TP的关系模型(三)

    这段时间对模型做了升级和优化,并将版本更新到TP3.2. 下载 下载后请将目录放置TP的Library目录下 1.数据节点优化,原来的节点为模型的名称或者表名,现在更新为定义关系的方法名 public ...

  4. diff函数的实现——LCS的变种问题

    昨天去去哪儿笔试,碰到了一个我们一直很熟悉的命令(diff——ubuntu下面),可以比较字符串,即根据最长公共子串问题,如果A中有B中没有的字符输出形式如下(-ch),如果A中没有,B中有可以输出如 ...

  5. npm 好用工具 for 前端

    1. caniuse npm install -g caniuse-cmd

  6. js 判断一个点是否在一个多边形之内

    出处: https://github.com/substack/point-in-polygon/blob/master/index.js github: https://github.com/sub ...

  7. hibernate中一对多 多对多 inverse cascade

    ----------------------------一对多------------------------------------------- inverse属性:是在维护关联关系的时候起作用的 ...

  8. 《Velocity java开发指南》中文版(下)转载

    文章出自:http://sakyone.iteye.com/blog/524292 8.Application Attributes Application Attributes (应用程序属性)是和 ...

  9. iOS: 学习笔记, Swift与Objective-C混用简明教程(转载)

    Swift与Objective-C混用简明教程 转载自:https://github.com/lifedim/SwiftCasts/tree/master/000_mix_swift_objc 我想很 ...

  10. ASP.NET MVC轻教程 Step By Step 6——改进表单

    上一节我们使用原始的HTML表单来完成留言功能,但是ASP.NET MVC提供了丰富的HTML辅助方法来帮助我们构建更简洁优雅的表单. Step 1. 修改Form标签 首先,我们可以使用Html.B ...