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.

InputThe 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. 
OutputFor 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 思路:很容易想到DFS,但第一次提交超时,说明需要剪枝,通过路径剪枝,奇偶剪枝,通过。
AC Code:
#include<iostream>
#include<utility>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
int INF=0x3f3f3f3f;
int N,M,T,sx,sy,gx,gy,flag,f;
char maze[][];
int vis[][];
int dx[]={,,-,},dy[]={,,,-};
void dfs(P p){
if(p.first ==gx&&p.second ==gy&&vis[p.first][p.second]==T){
flag=;
return;
}
if(vis[p.first ][p.second]>=T) return;//当前的步数超过要求的步数,退出
int mins=T-vis[p.first ][p.second]-abs(p.first-gx)-abs(p.second-gy);//最短距离
if(mins%||mins<) return; // 剩余步数小于最短距离或者满足奇偶剪枝条件
for(int i=;i<;i++){
int nx=p.first +dx[i],ny=p.second +dy[i];
if(nx>=&&nx<N&&ny>=&&ny<M&&maze[nx][ny]!='X'&&vis[nx][ny]==INF){
vis[nx][ny]=vis[p.first][p.second]+;
dfs(P(nx,ny));
vis[nx][ny]=INF;
}
} }
int main(){
while(~scanf("%d%d%d",&N,&M,&T),N+M+T){
int holdback=;flag=;
for(int i=;i<N;i++)
for(int j=;j<M;j++){
cin>>maze[i][j];
vis[i][j]=INF;
if(maze[i][j]=='S') sx=i,sy=j,vis[sx][sy]=;
if(maze[i][j]=='D') gx=i,gy=j;
if(maze[i][j]=='X') holdback++;//记录阻碍 X
}
if(N*M-holdback>T) dfs(P(sx,sy));//可通行的点必须大于要求的步数 路径剪枝
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl; }
}

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

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

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

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

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

随机推荐

  1. SpringBoot 整合携程Apollo配置管理中心

    携程官网对apollo的使用讲解了很多种方式的使用,但是感觉一些细节还是没讲全,特别是eureka配置中心地址的配置 这里对springboot整合apollo说一下 >SpringBoot启动 ...

  2. Linux配置ssh服务和XShell连接Linux

    SSH服务查看和安装,配置: https://www.cnblogs.com/qiuqiuqiu/p/6445426.html https://www.cnblogs.com/yunweis/p/77 ...

  3. SSM到Spring Boot从零开发校园商铺平台

    项目目的 特别 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Shop emmm, 已经 ...

  4. (转载)C# GDI+ 画简单的图形:直线、矩形、扇形等

    GDI+是一种绘图装置接口, 当拖动窗体是,窗体发生移动,window默认为从窗体移动到另一个地方,先发生擦除后再重新画一个窗体: 而我们自己动手画的图(如下面的线),不会重新画:在属性中,Paint ...

  5. Linux---centos 配置网络

    Linux配置网络,有两种方式,一种是通过图像化的界面来配置网络IP,另一种方式是通过命令行来配置IP 1.第一种方式通过图形化的界面来配置IP 1.0修改之前的IP地址 1.1点击图片中的那个 网络 ...

  6. C#中引用第三方ocx控件引发的问题以及解决办法

    调用OCX控件的步骤:1.在系统中注册该ocx控件,命令:regsvr32.exe 控件位置(加 /u 参数是取消注册)2.在.net的工具箱中添加该控件,拖到form中去就可以了. 不用工具箱的话, ...

  7. 每日质量NPM包复制_copy-to-clipboard

    一.copy-to-clipboard 官方定义: Simple module exposing copy function 理解: 一个超级简单的复制功能,并且这种方法适用于通过别的事件触发复制功能 ...

  8. 什么是 MIME TYPE?

    文章来源: http://baike.baidu.com/item/MIME https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_H ...

  9. restFul接口设计规范

    1.域名 1 应该尽量将API部署在专用域名之下. https://api.example.com 2 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https://example. ...

  10. java环境变量怎么配置

    我们在学习java的时候,必须先来配置一下java的环境变量,也许你不懂什么是java环境变量,我们也不需要懂,你只要知道,java环境变量配置好了,你的电脑就能编译和运行java程序了,这显然是你想 ...