Tempter of the Bone


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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


普通的搜索题,写完以后不幸TLE

之后看题解,发现这道题作为一道教程(完全没看)题,专门讲剪枝…… WTF!

于是加了个剪枝过了。300+ms,实际上可以各种花式剪枝把时间压到80ms,但是看写博客的时间就知道我为什么不想多写

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char map[20][20];
int mx[5]={0,-1,0,1,0},
my[5]={0,0,1,0,-1};
int n,m,Time;
int tx,ty;
int flag=0;
void dfs(int x,int y,int t){
if(x==tx && y==ty && t==Time){
flag=1;
return;
}
if(map[x][y]=='X')return;
//
if(t>=Time)return;//剪枝
int temp=(Time-t)-fabs(x-tx)-fabs(y-ty);
if(temp<0 ||temp %2)return; //
int i;
for(i=1;i<=4;i++){
int nx=x+mx[i];
int ny=y+my[i];
if(nx>=0 && nx<n && ny>=0 && ny<m)
// if(map[nx][ny]!='X')
{
map[x][y]='X';
dfs(nx,ny,t+1);
if(flag)return;
map[x][y]='.';
}
}
return;
}
int main(){
while(scanf("%d%d%d",&n,&m,&Time)!=EOF)
{
flag=0;
if(n==0 && m==0 && Time==0)break;
int i,j;
int xx,yy;
for(i=0;i<n;i++){
scanf("%s",map[i]);
for(j=0;j<m;j++){
if(map[i][j]=='S')xx=i,yy=j;
if(map[i][j]=='D')tx=i,ty=j;//目标点
}
}
dfs(xx,yy,0);
if(flag==1)printf("YES\n");
else printf("NO\n");
}
return 0;
}

ZOJ 2110 Tempter of the Bone的更多相关文章

  1. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  2. zoj 2110 Tempter of the Bone (dfs)

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

  3. ZOJ 2110 Tempter of the Bone(DFS)

    点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...

  4. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

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

  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

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

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

随机推荐

  1. 006医疗项目-模块一:用户的查找:2.用户表查询的mapper映射的文件

    前一篇文章已经把sql语句写好了并且在PL/SQL上调试过了,是可以的.这篇文章是写对应的mapper.xml, 第一步我们先通过逆向工程去构建每个表的mapper.xml文件和pojo类.这个我们在 ...

  2. android中scrollTo和scrollBy的理解

    protected   int  mScrollX;    //该视图内容相当于视图起始坐标的偏移量   , X轴 方向 protected   int  mScrollY;    //该视图内容相当 ...

  3. vim常用命令汇总

    vim常用命令汇总: http://www.cnblogs.com/softwaretesting/archive/2011/07/12/2104435.html 定位 本行第一个字符 ctrl+$ ...

  4. MySQL学习指引

    mysql指引 1,mysql基本安装 2,mysql多实例安装与维护 3,备份恢复 备份数据库 分备数据库 分备表 恢复数据库

  5. XPath 详解,总结

    XPath简介 XPath是W3C的一个标准.它最主要的目的是为了在XML1.0或XML1.1文档节点树中定位节点所设计.目前有XPath1.0和XPath2.0两个版本.其中Xpath1.0是199 ...

  6. ZooKeeper学习第四期---构建ZooKeeper应用

    一.配置服务 配置服务是分布式应用所需要的基本服务之一,它使集群中的机器可以共享配置信息中那些公共的部分.简单地说,ZooKeeper可以作为一个具有高可用性的配置存储器,允许分布式应用的参与者检索和 ...

  7. Caffe学习系列(18): 绘制网络模型

    python/draw_net.py, 这个文件,就是用来绘制网络模型的.也就是将网络模型由prototxt变成一张图片. 在绘制之前,需要先安装两个库 1.安装GraphViz # sudo apt ...

  8. 在matlab和opencv中分别实现稀疏表示

    在本文中,稀疏表示的原理不再具体讲解,有需要的同学请自行百度. 本文采用OMP算法来求解稀疏系数.首先随机生成字典数据和待测试数据 字典数据: dic =[ 6, 7, 9, 9, 7, 0, 6, ...

  9. Java 通过JDBC查询数据库表结构(字段名称,类型,长度等)

    Java 通过JDBC查询数据库表结构(字段名称,类型,长度等) 发布者:唛唛家的豆子   时间:2012-11-20 17:54:02   Java 通过JDBC查询数据库表结构(字段名称,类型,长 ...

  10. Linux常用指令---tar | zip (解压缩)

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...