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
 
用深度搜索和奇偶剪枝即可解决
奇偶剪枝-百度百科(里面原理补充部分讲的很好理解):https://baike.baidu.com/item/%E5%A5%87%E5%81%B6%E5%89%AA%E6%9E%9D/10385689
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace::std; int N,M,T,t;
char maps[][];
int maps_[][];
int stx,sty,enx,eny;
int a[][] = {{,},{-,},{,},{,-}};
bool dfs(int n,int m)
{
if( n == enx && m == eny && t == T )
{
return true;
} int ans = T-t-abs(enx-n)-abs(eny-m); //奇偶剪枝,感觉好吊。
if(ans< || ans&)
return false; for(int i = ;i<;i++)
{
int x = n + a[i][];
int y = m + a[i][];
if(maps[x][y] != 'X' && maps_[x][y] != && x>= && y>= && x<N && y<M)
{
t++;
maps_[x][y] = ; if(dfs(x,y))
return true;
else{
t--;
maps_[x][y] = ;
} }
}
return false;
}
int main()
{
while(scanf("%d %d %d",&N,&M,&T) && N != )
{
memset(maps,,sizeof(maps)); //每次输入把地图和标记清空
memset(maps_,,sizeof(maps_));
for(int i=; i<N; i++)
{
scanf("%s",&maps[i]);
for(int j = ; j<M;j++)
{
if(maps[i][j] == 'S') //起始点
{
stx = i;
sty = j;
}
else if(maps[i][j] == 'D') //终点
{
enx = i;
eny = j;
}
}
}
t = ;
maps_[stx][sty] = ; //标记起点
if(dfs(stx,sty))
{
printf("YES\n");
}else
printf("NO\n");
}
return ;
}

hdoj 1010-Tempter of the Bone的更多相关文章

  1. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

  2. hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

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

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

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

  4. HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

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

  5. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

  6. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

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

  7. hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)

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

  8. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

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

  9. hdu 1010 Tempter of the Bone 深搜+剪枝

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

  10. hdu 1010 Tempter of the Bone(dfs)

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

随机推荐

  1. python取字母以及数字随机数

    一.这里用到了:String模块ascii_letters和digits ''.join(random.sample(string.ascii_letters + string.digits, 9)) ...

  2. js --装饰者模式

    定义 装饰者模式能够在补改变对象自身的基础上,在程序运行期间给对象动态的添加职责. 当看到装饰者模式的定义的时候,我们想到的js 的三大特性之一--继承,不也能够实现不改变对象自身的基础上,添加动态的 ...

  3. jupyter安装出现问题:安装后无法打开

    jupyter安装出现问题:安装后无法打开 traitlets.traitlets.TraitError: Could not decode 'C:\Users\\xce\xa2\xcc\xf0\xd ...

  4. Android笔记(四十三) Android中的数据存储——SQLite(五)delete

    SQLite通过delete()方法删除数据 delete()方法参数说明: delete()方法参数 对应sql部分 描述 table delte from table_name 要删除的表 whe ...

  5. 用python实现数据库查询数据方法

    哈喽,好久没来了,最近搞自动化发现了很多代码弯路,特别分享出来给能用到的朋友 因为公司业务的关系,每做一笔功能冒烟测试,我们就要对很多的数据库表中的字段进行校验,当时我就想反正总是要重复的运行这些SQ ...

  6. springboot2.1.3+Junit4 单元测试

    引入依赖的包: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core ...

  7. IDEA实用教程(七)—— IDEA的断点调试

    IDEA实用教程(七)-- IDEA的断点调试 23/100 发布文章 qq_41684621 六. IDEA的断点调试 打断点 在行号的右侧点击鼠标左键,出现红色圆形图标,说明已经被打上断点 Deb ...

  8. 让 Python 代码更易维护的七种武器——代码风格(pylint、Flake8、Isort、Autopep8、Yapf、Black)测试覆盖率(Coverage)CI(JK)

    让 Python 代码更易维护的七种武器 2018/09/29 · 基础知识 · 武器 原文出处: Jeff Triplett   译文出处:linux中国-Hank Chow    检查你的代码的质 ...

  9. P2756 飞行员配对方案问题[二分图最大匹配]

    题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中,每一名 ...

  10. 【Python学习】Python3 基本数据类型

    参考学习地址:https://www.runoob.com/python3/python3-data-type.html Python3 基本数据类型 Python 中的变量不需要声明.每个变量在使用 ...