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.
'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.
#include <iostream>
#include<cstdio>
using namespace std;
char maze[][];//保存地图信息
int n,m,t;//地图大小n*m,起点到终点能否恰好为t
bool success;//是否找到所需状态标记
int go[][]={//s四个方向行走坐标差
-,,
,,
,,
,-
}; void DFS(int x,int y,int time){
for(int i=;i<;i++){
int nx=x+go[i][];//枚举四个相邻位置
int ny=y+go[][i];
if(nx< || nx>n || ny< || ny>m)//地图外
continue;
if(maze[nx][ny]=='X')//碰墙
continue;
if(maze[nx][ny]=='D'){//到终点
if(time+==t){//所用时间恰好为t
success=true;//搜索成功
return;
}
else
continue;
}
maze[nx][ny]='X';//该点设为墙
DFS(nx,ny,time+);//递归扩展该状态
maze[nx][ny]='.';//把原来的路改回来
if(success)
return;
}
} int main(){
while(scanf("%d %d %d",&n,&m,&t)!=EOF){
if(n== && m== && t==)
break;
for(int i=;i<=n;i++)
scanf("%s",maze[i]+);
success=false;
int sx,sy;
for(int i=;i<=n;i++){//寻找D的坐标
for(int j=;j<=m;j++){
if(maze[i][j]=='D'){
sx=i;
sy=j;
}
}
}
for(int i=;i<=n;i++){//找到S后,判断S和D的奇偶关系是否和t相符
for(int j=;j<=m;j++){
if(maze[i][j]=='S' && (i+j)%==((sx+sy)%+t%)%){
maze[i][j]='X';//起始点设为墙
DFS(i,j,);//递归扩展初始状态
}
}
}
puts(success==true?"YES":"NO");
}
return ;
}
//说实话我真的不是很懂43行,为什么要+1。。。请在评论区告诉我,多谢!
Tempter of the Bone——DFS(王道)的更多相关文章
- 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 ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- HDOJ.1010 Tempter of the Bone (DFS)
Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- doxygen使用
前言 下面主要讲解linux下Doxygen命令行实现html文档生成的操作,当然也有界面版本操作方式,linux下安装doxygen-gui即可通过doxywizard开启界面操作,windows下 ...
- appium的使用
这套教程年久失修,问题的人也比较多,于是,我重新整理了一套appium入门教程. appium新手入门(1)—— appium介绍 appium新手入门(2)—— 安装 Android SDK app ...
- Android之BInder分析
MediaService的诞生: nt main(int argc, char** argv) { //FT,就这么简单?? //获得一个ProcessState实例 sp<ProcessS ...
- P1489 猫狗大战
P1489 猫狗大战 题目描述 新一年度的猫狗大战通过SC(星际争霸)这款经典的游戏来较量,野猫和飞狗这对冤家为此已经准备好久了,为了使战争更有难度和戏剧性,双方约定只能选择Terran(人族)并且只 ...
- mysql查看每张表的空间使用情况
use information_schema; /,),'MB') as data ,concat(round(index_length//,),'MB') as indexweight from T ...
- CSS基本属性—文本属性和背景属性
一.CSS常用文本属性 [css中的颜色表示方式] 1.直接使用颜色的单词表示:red.green.blue 2.使用颜色的十六进制表示:#ff0000,#00ff00: 六位数,两两 ...
- Spring:面向切片编程
在之前我们记录Spring的随笔当中,都是记录的Spring如何对对象进行注入,如何对对象的属性值进行注入,即我们讲解的很大部分都是Spring的其中一个核心概念——依赖注入(或者说是控制翻转,IOC ...
- 51nod 1283 最小周长【注意开根号】
1283 最小周长 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 一个矩形的面积为S,已知该矩形的边长都是整数,求所有 ...
- 学习LSM(Linux security module)之二:编写并运行一个简单的demo
各种折腾,经过了一个蛋疼的周末,终于在Ubuntu14.04上运行了一个基于LSM的简单demo程序. 一:程序编写 先简单的看一下这个demo: //demo_lsm.c#include <l ...
- 16、Flask实战第16天:Flask文件上传
上传文件 这里以图片来演示flask如何上传文件 首先来写个简单的前端页面upload.html, 在里面写个文件上传的表单 后端debug断点调试 获取到文件后,当然,我们需要把文件保存下来.在项目 ...