Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58766    Accepted Submission(s): 15983

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
 

Author

ZHANG, Zheng
 

Source

 

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1241 1242 1072 1312 1026 
 

  这道题是一道搜索基本题,做法是DFS+剪枝。要注意剪枝要用到奇偶剪枝,否则提交会超时。
  虽然是道基本题,但是也做了近2个小时,主要时间都花在了剪枝的处理上。通过这道题学到了奇偶剪枝,不知道奇偶剪枝的可以猛戳后面的链接:
题意:
  输入一个n*m的矩阵以及时间T(0<n,m<7,0 < T < 50)。
  矩阵中有'X':墙,不能走;'.':通路,可以走;'S':开始点。'D':门,结束点。
  要求从开始点开始走,每秒一步,走过的路不能再走,走到D的时候正好花费时间T。
  输出“YES”或“NO”表示能否在时间T的时候走到D。
代码:
 #include <iostream>
#include <cstring>
using namespace std;
char maze[][];
int isw[][];
int dx[]={,,,-};
int dy[]={,,-,};
int N,M,T;
int curx,cury,endx,endy;
int abs(int n)
{
return n>=?n:-n;
}
int dfs(int curx,int cury,int curt) //判断从当前位置(curx,cury)能否用刚好curt时间到达结束点
{
if(curt==){ //当时间耗尽的时候,判断是否到达了结束点
if(curx==endx && cury==endy)
return ;
else
return ;
}
//剪枝 1 :奇偶剪枝
int m = abs(curx-endx) + abs(cury-endy); //理想情况下,开始点到终点的最小步数
int t = curt; //要求走t步正好走到终点
//当 t<m 时,一定不能到达
//当 t>=m 时,要用t步从开始点正好走到终点,分两部分。
// 一部分为最小步数 m ,另一部分是为了凑够t步而余外多走的几步,设为 a。
// 而走出去就一定要走回来,所以走出去的步数和回来的步数一定是相等的,为b步。
// a=2b,所以多走的a步一定是偶数。
// 这里的奇偶剪枝就是判断 a 是否为偶数。如果不是偶数,一定不能到达。
if( t<m || (t-m)& )
return ;
for(int i=;i<;i++){ //剪枝 2
if( <=curx+dx[i] && curx+dx[i]<=N && <=cury+dy[i] && cury+dy[i]<=M //如果没有越界
&& !isw[curx+dx[i]][cury+dy[i]] //如果下一步没有走过
&& maze[curx+dx[i]][cury+dy[i]]!='X' ){ //如果下一步不是墙
isw[curx+dx[i]][cury+dy[i]]=true;
if(dfs(curx+dx[i],cury+dy[i],curt-)) //如果下一步这样走可以生存,则返回1
return ;
isw[curx+dx[i]][cury+dy[i]]=false;
}
}
return ;
} int main()
{
while(cin>>N>>M>>T){
if(N== && M== && T==) break;
memset(isw,,sizeof(isw)); //将isw[][]数组初始化为false,表示还没有走过。
for(int i=;i<=N;i++)
for(int j=;j<=M;j++){
cin>>maze[i][j];
if(maze[i][j]=='S'){ //记录开始点的位置
curx=i;
cury=j;
}
else if(maze[i][j]=='D'){ //记录终点的位置
endx=i;
endy=j;
}
}
isw[curx][cury]=true; //开始位置赋true
if(dfs(curx,cury,T)) //如果能够生存,输出YES,否则输出NO
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)的更多相关文章

  1. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  2. 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 ...

  3. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

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

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

  5. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

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

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

  7. Tempter of the Bone(dfs奇偶剪枝)

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

  8. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  9. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  10. (step4.3.1) hdu 1010(Tempter of the Bone——DFS)

    题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...

随机推荐

  1. Boost的状态机库教程(1)

    介绍 Boost状态机库一个应用程序框架,你可以用它将UML状态图快速的转换为可执行的c++代码,而不需要任何的代码生成器.它支持几乎所有的UML特征,可以直接了当的转换,并且转换后的c++代码就像对 ...

  2. hdu 1049 Climbing Worm

    解题思路: 1. 两种情况,0x1:井深度小于一次跳的高度.0x2:井深度大于一次跳的高度 2.如果 属于 0x1 则一次跳出 3.否则 本次解题中直接枚举跳的次数 一直循环,直到 [每次跳的真实高度 ...

  3. hdu 5747 Aaronson

    T :  1 n m:  10  2 题解:20 * 0  +  21* 1  +  22* 2 = 10 输出:3  <--  0+1+2=3 AC 代码: #include<stdio ...

  4. ReactiveCocoa初步

    [self.usernameTextField.rac_textSignal subscribeNext:^(id x) { NSLog(@"%@", x); }]; 打印结果 - ...

  5. [LeetCode] Copy List with Random Pointe

    题目的关键是要让新链表和原有链表发送关联,可以通过这种关联来设置新链表的random pointer 思路:将新链表的元素插入到原有链表元素的后面,如下图所示,就可以根据原有链表的radom-> ...

  6. SQL Server 2005导入Excel表问题

    EXCEL导入到SQL Server经常出现“文本被截断,或者一个或多个字符在目标代码页中没有匹配项” 原因: SQL Server的导入导出为了确定数据表的字段类型,取excel文件的前8行来判别. ...

  7. Sublime Text2 快捷键 (MAC版)

    工具是人的延伸,可以把人变得更聪明更强大,人类正是学会了使用工具,才创造出现在的文明.作为程序员,工具开发.使用是其能力的重要体现,业内的大牛都是造工具的好手.目前身边很多人都在用sublime te ...

  8. 敲点JavaScript代码

    1. DOM DEMO-表格的行排序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  9. webservice远程调试开启

    在.NET 中已经默认将WEBSERVICE的远程调试功能关闭,有的时候我们需要远程调试程序的时候,就需要打开此功能我们只需在WEBSERVICE的项目的中添web.config的<system ...

  10. C/C++语法知识:typedef struct 用法详解

    第一篇:typedef struct与struct的区别 1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定 ...