Tempter of the Bone dfs+剪枝
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.
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,剪枝很重要。避免做一些不必要的判断,还有起点不可以再走了。 代码:
#include <iostream>
#include <cstdlib>
using namespace std; int flag,n,m,t,sx,sy,ex=-,ey=-,dir[][]={,,,,,-,-,},vis[][];
char ar[][];
void dfs(int x,int y,int time)
{
char ch;
if(ar[x][y]=='D')
{
if(time==t)flag=;
return;
}
if((t-time-x-y+ex+ey)&)return;///奇偶剪枝 以坐标和判断奇偶数
if(flag||time>t)return;
int tx,ty;
for(int i=;i<;i++)
{
if(flag)return;
tx=x+dir[i][],ty=y+dir[i][];
if(tx<||ty<||tx>=n||ty>=m||ar[tx][ty]=='X'||vis[tx][ty])continue;
ch=ar[tx][ty];
vis[tx][ty]=;
dfs(tx,ty,time+);
vis[tx][ty]=;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
while(cin>>n>>m>>t)
{
if(!n&&!m&&!t)break;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>ar[i][j];
if(ar[i][j]=='S')sx=i,sy=j;
else if(ar[i][j]=='D')ex=i,ey=j;
}
}
flag=;
ar[sx][sy]='X';//起点一定要标记 不可以再走了!!!!!!!!!
dfs(sx,sy,);
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
Tempter of the Bone dfs+剪枝的更多相关文章
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- B - Tempter of the Bone(DFS+剪枝)
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- 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 ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- HDOJ.1010 Tempter of the Bone (DFS)
Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...
- 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 ...
随机推荐
- Linux中su、su -和sudo的区别
su 切换到root用户,但是并没有转到root用户家目录下,即没有改变用户的环境. su - 切换到root用户,并转到root用户的家目录下,即改变到了root用户的环境. 这个涉及到不同用户下的 ...
- python-day49--前端 html
一.列表标签 1.有序列表 <ol> (order list ) 在浏览器中显示包括:padding , 有序排列 <li>:列表中的每一项. 2.无序列表 ...
- python-day9-数据类型总结
数据类型总结: 常用: 数字 字符串 列表 元组 字典 不常用:集合 1.按照存值个数: 1个:数字,字符串 多个:列表,元组,字典,(集合) 2.按照可变不可变: 可变:列表,字典,(集合) 不可 ...
- ORACLE11G内存管理参数
今天,对ORACLE11G的几个内存参数看了一下,记录如下,大家可以参考: 1.首先,在ORACLE11G的INIT.ORA里,有“__”开头的参数,也就是以两个下划线开头的参数,这种参数应该是系统自 ...
- spark 任务运行原理
调优概述 在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以 ...
- 进程通信方式-管道pipe
管道是两个进程间进行单向通信的机制.因为管道传递数据的单向性,管道又称之为半双工管道. 1.数据只能从一个进程流向另一个进程(其中一个写管道,另一个读管道):如果要进行全双工通信,需要建立两个管道. ...
- 文件属性,获取,设置文件属性chown stat函数
转载:http://c.biancheng.net/cpp/html/326.html man 2 stat查看手册 int stat(const char *path, struct stat *b ...
- Java live template[在此处输入文章标题]
Java -Dfile.encoding=UTF-8 提示键盘 功能 Logg private final Logger log = Logger.getLogger(this.getClass()) ...
- photoshop cc 智能切图
这节分享一个photoshop cc 开始有的自动生成图标的方法 psd练习文件 http://pan.baidu.com/s/1pL2dwL1 1 工具:我这里用的是photoshop cc 201 ...
- spoj375
题解: 树链剖分的模板题 具体代码详见网上的其他代码 代码: #include<cstdio> #include<cmath> #include<cstring> ...