ZOJ 2110 Tempter of the Bone
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的更多相关文章
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
题意 一仅仅狗要逃离迷宫 能够往上下左右4个方向走 每走一步耗时1s 每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次 问狗是否有可能逃离这个迷宫 直接DFS 直道找到满足条件的路径 ...
- 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 ...
- ZOJ 2110 Tempter of the Bone(DFS)
点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- 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 ...
- 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 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- 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 ...
- 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 ...
随机推荐
- java 21-13 合并
SequenceInputStream(Enumeration<? extends InputStream> e) 通过记住参数来初始化新创建的 SequenceInp ...
- VC2010中去掉红绝下划线
1. 在VC2010 的tools/中找到editior. 已经找到c/c++->advanced->IntelliSense->Disable IntelliSence->T ...
- 通过imeMode禁用键盘只能输入数字
var obj = document.getElementById('y'); var arr = [48,49,50,51,52,53,54,55,56,57];//数字对应的键码 obj.onke ...
- javascript:让表单 文本框 只读,不可编辑的方法
有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...
- setAttribute改变属性,动态改变类
<style type="text/css"> .box{color:red;} </style> <div>通过setAttribute添加d ...
- iOS开发——网络编程Swift篇&Alamofire详解
Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...
- mobileTech
A useful tools or tips list for mobile web application developing 这个项目收集移动端开发所需要的一些资源与小技巧 工具类网站 HTML ...
- 014医疗项目-模块一:删除用户的功能l
删除用户的功能我们还是按照:Dao->Service->Action->页面调试这种顺序来写. Dao: 我们使用逆向工程生成的方法就好: SysuserMapper sysuser ...
- 无法解析此远程名称: 'www.***.com' 解决办法 请求因 HTTP 状态 417 失败
今天在做接口开发时,遇到了一个异常:无法解析此远程名称: 'www.***.com'.我的网站一直是运行正常的,从昨天开始出现异常,用户可以使用,但我的服务器怎么也无法实现对数据库的更新. 分析原因: ...
- java系列:《java核心技术 卷1》学习笔记,chapter 11 调试技巧
11. 6 调试技巧 1)一个不太为人所知却非常有效的技巧是在每个类中放一个main方法,这样就可以对每个类进行单元测试.这个方法可以保留,因为在java虚拟机只调用启动类的main方法. 2) ...