HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 125945 Accepted Submission(s): 33969
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 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.
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
这道题可以不剪枝操作,也不会T了!
不懂剪枝的看这里:DFS中的奇偶剪枝学习笔记
这里说下不剪枝的技巧:为了避免多余的边界控制,可以从i=1,j=1开始读迷宫,在读之前将迷宫初始化为全部'X',即都为墙。这样在迷宫读取完毕后,周围就会自动出现一圈'X',这样就可以在搜索的时候只判断遇到'X'就return了。
这里贴一下深搜代码,不管剪不剪枝,这一段是可以不用修改的。
inline int DFS(int x,int y,int T)
{
if(mp[x][y]!='.'&&mp[x][y]!='S')//碰到X即为边界返回
return ;
if(T==)//剩一步时即可判断是否为出口,找到返回1
{
if(mp[x-][y]=='D')
return ;
if(mp[x+][y]=='D')
return ;
if(mp[x][y-]=='D')
return ;
if(mp[x][y+]=='D')
return ;
return ;
}
else
{
mp[x][y]='X';//标记走过
if(mp[x-][y]=='.'&&DFS(x-,y,T-))
return ;
if(mp[x+][y]=='.'&&DFS(x+,y,T-))
return ;
if(mp[x][y-]=='.'&&DFS(x,y-,T-))
return ;
if(mp[x][y+]=='.'&&DFS(x,y+,T-))
return ;
mp[x][y]='.';//还原走过
return ;
}
return ;
}
关于奇偶剪枝
首先举个例子,有如下4*4的迷宫,'.'为可走路段,'X'为障碍不可通过
S...
....
....
...D
从S到D的最短距离为两点横坐标差的绝对值+两点纵坐标差的绝对值 = abs(Sx - Dx) + abs(Sy - Dy) = 6,这个应该是显而易见的。
遇到有障碍的时候呢
S.XX
X.XX
...X
...D
你会发现不管你怎么绕路,最后从S到达D的距离都是最短距离+一个偶数,这个是可以证明的
而我们知道:
奇数 + 偶数 = 奇数
偶数 + 偶数 = 偶数
因此不管有多少障碍,不管绕多少路,只要能到达目的地,走过的距离必然是跟最短距离的奇偶性是一致的。
所以如果我们知道从S到D的最短距离为奇数,那么当且仅当给定的步数T为奇数时,才有可能走到。如果给定的T的奇偶性与最短距离的奇偶性不一致,那么我们就可以直接判定这条路线永远不可达了。
这里还有个小技巧,我们可以使用按位与运算来简化奇偶性的判断。我们知道1的二进制是1,而奇数的二进制最后一位一定是1,而偶数的二进制最后一位一定是0。所以如果数字&1的结果为1,那么数字为奇数,反之为偶数。
下面给出奇偶剪枝后的main函数代码:
int main()
{
int sx,sy,dx,dy;
int N,M,T;
while(scanf("%d%d%d",&N,&M,&T)&&N&&M&&T)
{
getchar();
memset(mp,'X',sizeof(mp));//把周围边界全部变成X
for(int i=;i<=N;i++)//从1开始读,留出边界位置
{
for(int j=;j<=M;j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='S')
{
sx=i;
sy=j;
}
else if(mp[i][j]=='D')
{
dx=i;
dy=j;
}
}
getchar();
}
if((abs(sx-dx)+abs(sy-dy)-T)&)//奇偶剪枝,对1用按位与运算求奇偶
printf("NO\n");
else if(DFS(sx,sy,T)==)
printf("YES\n");
else printf("NO\n");
}
return ;
}
所以完整的AC代码如下:
#include <bits/stdc++.h>
using namespace std;
char mp[][];
inline int DFS(int x,int y,int T)
{
if(mp[x][y]!='.'&&mp[x][y]!='S')//碰到X即为边界返回
return ;
if(T==)//剩一步时即可判断是否为出口,找到返回1
{
if(mp[x-][y]=='D')
return ;
if(mp[x+][y]=='D')
return ;
if(mp[x][y-]=='D')
return ;
if(mp[x][y+]=='D')
return ;
return ;
}
else
{
mp[x][y]='X';//标记走过
if(mp[x-][y]=='.'&&DFS(x-,y,T-))
return ;
if(mp[x+][y]=='.'&&DFS(x+,y,T-))
return ;
if(mp[x][y-]=='.'&&DFS(x,y-,T-))
return ;
if(mp[x][y+]=='.'&&DFS(x,y+,T-))
return ;
mp[x][y]='.';//还原走过
return ;
}
return ;
}
int main()
{
int sx,sy,dx,dy;
int N,M,T;
while(scanf("%d%d%d",&N,&M,&T)&&N&&M&&T)
{
getchar();
memset(mp,'X',sizeof(mp));//把周围边界全部变成X
for(int i=;i<=N;i++)//从1开始读,留出边界位置
{
for(int j=;j<=M;j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='S')
{
sx=i;
sy=j;
}
else if(mp[i][j]=='D')
{
dx=i;
dy=j;
}
}
getchar();
}
if((abs(sx-dx)+abs(sy-dy)-T)&)//奇偶剪枝,对1用按位与运算求奇偶
printf("NO\n");
else if(DFS(sx,sy,T)==)
printf("YES\n");
else printf("NO\n");
}
return ;
}
HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】的更多相关文章
- hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)
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(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- 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+可行性奇偶剪枝)
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- HDOJ.1010 Tempter of the Bone (DFS)
Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...
随机推荐
- 串String(2):串的实现(堆分配存储表示法)
7/27/2017,先占个位,最近事情比较忙,明天敲一波代码,预测在一星期内搞定 9/02/2017,看到这个十分汗颜,八月去美帝学习了,没有抽空补上这一博文,计划这个月开了数据结构课后补上
- Git安装和使用(谨记)
刚开始用git的小白适用,,转自http://www.cnblogs.com/qijunjun/p/7137207.html 实际项目开发中,我们经常会用一些版本控制器来托管自己的代码,今天就来总结下 ...
- 用python在excel中读取与生成随机数写入excel中
今天是我第一次发博客,就关于python在excel中的应用作为我的第一篇吧. 具体要求是:在一份已知的excel表格中读取学生的学号与姓名,再将这些数据放到新的excel表中的第一列与第二列,最后再 ...
- Web API系列之二WebApi基础框架搭建
本文主要介绍如何搭建一个WebApi的项目.关于如何搭建WebApi的方式一共有两种: 一.通过vs直接新建一个WebApi的项目,步骤如下: 第一步: 新建一个空的Web应用程序,可以理解为作为We ...
- JAVA 用数组实现 ArrayList
我们知道 ArrayList 是一个集合,它能存放各种不同类型的数据,而且其容量是自动增长的.那么它是怎么实现的呢? 其实 ArrayList 的底层是用 数组实现的.我们查看 JDK 源码也可以发现 ...
- [编织消息框架][网络IO模型]Netty Reactor
严格来讲Netty Reactor是一种设计模式,一听模式两字就知道了吧,套路哈哈 Reactor中文译为“反应堆”. 看图netty处理流程 1.netty server 至少有两组reactor. ...
- 【http转https】其之三 IIS_URL重写_http重定向到https
IIS_URL重写_http重定向到https 文:铁乐猫 2016年1月14日 IIS7以上支持URL Rewrite这个模块了,所以在我们做好了ssl证书这一块之后, 要对来自http的请求重定向 ...
- 基于zepto的移动端日期和时间选择控件
前段时间给大家分享过一个基于jQuery Mobile的移动端日期时间拾取器,大家反应其由于加载过大的插件导致影响调用速度.那么今天我把从网络上搜集到的两个适合移动端应用的日期和时间选择插件分享给大家 ...
- MySQL 数据类型和约束(外键是重点🙄)
数据类型 1. 数字(默认都是由符号,宽度表示的是显示宽度,与存储无关).tinyint 括号里指定宽度 七位2进制数最大数就是2**7 -1=127 最小是-128 验证: create tabel ...
- MySQL如何找到表与表之间的关系?
如何找到两张表之间的关系? 先站在左表的角度上去找,如果可以找到左表的多个字段可以对应右表的一个字段,那么左表的一个字段foregin key右表的一个字段.一般情况下为id... 2.如果右表的多个 ...