Tempter of the Bone HDU 1010(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<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char maps[][];
int n,m,t;
int di,dj;//出口
int flag;//是否成功脱逃
int dir[][]= {{,-},{,},{,},{-,}}; //四方搜索
void DFS(int x,int y,int cnt)
{
int i,temp;
int a,b;
if (x>n || y>m || x<= || y<=)//越出边界便不搜索
{
return;
}
if (x==di && y==dj && cnt==t) //时间正好的时候才能逃生
{
flag=;
return;
}
temp=abs(t-cnt)-(abs(di-x)+abs(dj-y));
//计算当前到终点的最短路与还需要的时间差,若小于0则路径剪枝
if (temp< || temp%)//temp如果是奇数的话也要剪枝
{
return;//不符合条件
}
for (i=; i<; i++)
{
a=x+dir[i][];
b=y+dir[i][];
if (maps[a][b]!='X')
{
maps[a][b]='X';//前进方向!
DFS(a,b,cnt+);//搜索该点
if (flag)
{
return;
}
maps[a][b]='.';//后退方向!恢复现场!
}
}
return ;
}
int main()
{
int i,j;
int wall;
int si,sj;//起点
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==&&m==&&t==)
{
break;
}
getchar();
wall=;
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
scanf("%c",&maps[i][j]);
if(maps[i][j]=='S')
{
si=i;
sj=j;
}
else if(maps[i][j]=='D')
{
di=i;
dj=j;
}
else if(maps[i][j]=='X')
{
wall++;
}
}
getchar();
}
if(n*m-wall<=t)//路径剪枝,当走完不含墙的迷宫都还不到t时间将不能逃生
{
printf("NO\n");
continue;
}
flag=;
maps[si][sj]='X';//刷为x
DFS(si,sj,);
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
}
Tempter of the Bone HDU 1010(DFS+剪枝)的更多相关文章
- 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 ...
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
传送门: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010 ZOJ:http://acm.zju.edu.cn/onlinejudge/showPr ...
- Tempter of the Bone HDU - 1010
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...
- hdu 1010 dfs搜索
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
题意 一仅仅狗要逃离迷宫 能够往上下左右4个方向走 每走一步耗时1s 每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次 问狗是否有可能逃离这个迷宫 直接DFS 直道找到满足条件的路径 ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- 题目1461:Tempter of the bone(深度优先遍历DFS)
题目链接:http://ac.jobdu.com/problem.php?pid=1461 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- hdu 4109 dfs+剪枝优化
求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己添加的)出发,0到1~n个节点之间的距离为1.mt[i]表示从0点到第i个节点眼下所得的最长路径 #include<iost ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
随机推荐
- BUAA_OO第二单元总结性博客作业——多线程电梯架构
一.设计策略 在第一次作业时,我刚第一次接触多线程这个东西……于是乎对于第一次VIP直上直下一次只接一个人的电梯,我借鉴了指导书中为我们提供的架构,设计了一个输入线程和一个电梯线程,并设置了一个中间类 ...
- XML的序列化用法 vs平台开发
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { NewMethod(); } } #region 读取 ...
- ionic3 监听软键盘的高度
ionic1 和普通cordova的大家都知道 就是看ionic3 和4 https://blog.csdn.net/sean_css/article/details/70243893 ionic c ...
- Linux基础-5.利用vi编辑器创建和编辑正文文件
1.vi编辑器简介 1)掌握vi编辑器的定义:vi编辑器是Linux和Unix上最基本的文本编辑器,工作在字符模式下.由于不需要图形界面,vi是效率很高的文本编辑器.尽管在Linux上也有很多图形界面 ...
- Angular 弹窗 控件
这个控件个人很喜欢,比起primgNG等弹窗组建,这款弹窗可以很轻松的定义自己的样式和布局. 可控参数有:宽度,高度,是否带有关闭图标,基本满足基础弹窗需求. 并且 Title/Content/Foo ...
- 大数据时代的结构化存储--HBase
迄今,相信大家肯定听说过 HBase,但是对于 HBase 的了解可能仅仅是它是 Hadoop 生态圈重要的一员,是一个大数据相关的数据库技术. 今天我带你们一起领略一下 HBase 体系架构,看看它 ...
- day 32 管道,信号量,进程池,线程的创建
1.管道(了解) Pipe(): 在进程之间建立一条通道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象,强调一点:必须在产生Process对象之前产生管道. ...
- Python实现注册和三次验证登录
# 帐户表account:# sylar:123# alex:456# wusir:789# taibai:789# 需熟练的知识点:文件操作with open()/write()/read().去掉 ...
- 浅谈ruby中的block及yield
今天写代码的时候遇到了block_given?,查阅了一下语法书中并没有相关的知识点,于是翻阅微博及结合工作中的实际代码,整理如下: 一.“块”: ruby的块指的是什么? 是 do~end中间的那部 ...
- Oracle笔记之——常用的函数及脚本
一.oracle 常用的函数及关键字 1.集合操作 1)minus 差集 2)intersect 交集 3)UNION 并集,会去重 4)UNION ALL 并集,不去重2.事物 1)COMMIT ( ...