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. 集训总结DAY.1(18.5.22)——KMP

    DAY 1——5.22 in the morning 依稀记得我们有一场contest. at night chf大佬讲KMP,先膜一波~~~ luoguP3375KMP模板题 KMP算法,又称模式匹 ...

  2. 关于link标签的用法, 不声明rel=stylesheet则无效? 在ff中必须声明rel属性!

    void 无效的, 空的; invalid: 无效的, void 和 invalid 在表示无效的时候, 是一样的, 等同的 the treaty (条约) was declared invalid ...

  3. Dockerfile 收集

    Dockerfile 收集 1.最基础的oracle-jdk FROM centos:7.2.1511 MAINTAINER hongdada "hongdaqi159505@gmail.c ...

  4. c 语言中宏定义和定义全局变量的区别

    宏定义和定义全局变量的区别: 1 作用时间不同. 宏定义在编译期间即会使用并替换,而全局变量要到运行时才可以. 2 本质类型不同. 宏定义的只是一段字符,在编译的时候被替换到引用的位置.在运行中是没有 ...

  5. 给斐讯K1刷机并拨号e信(湖北地区测试无问题)

    ◆购买斐讯k1路由器 路由器在天猫京东斐讯旗舰店都有售卖,我买的价格是159,不过有一张铃铛卡,一个月之后返还160元,相当于0元购 ◆路由器刷不死Breed 1.路由与电脑有线连接好,输入192.1 ...

  6. HIHOcoder 1449 后缀自动机三·重复旋律6

    思路 显然endpos的大小就对应了对应子串的出现次数,所以快速求出endpos的大小,然后用它更新对应子串长度(minlen[i]~maxlen[i])的答案即可 endpos的大小可以拓扑排序求出 ...

  7. JavaWeb 基础学习

    XMAPP是自己封装的一套 web 开发套件 —— 例如Tomcat等是用自己的,而不是使用系统中其他地方安装好了的.此外将提供的 xampp 工具解压到 D 盘根目录下.(注意 xampp 一定要解 ...

  8. Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  9. 在js文件里调用另一个js文件里的函数

    这个是我今天解决的一个小问题,我在创建界面的时候,根据不同的界面需求对应创建了不同的js文件来搭建界面,搭建完毕之后再将各个生成页面的函数汇总到主界面上,通过visibility属性切换显示,这时候出 ...

  10. Tp5.1使用导出Excel

    composer require phpoffice/phpexcel 不管它的警告,都能用的. use PHPExcel; use PHPExcel_IOFactory; public static ...