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 ...
随机推荐
- UML2和建模工具学习总结
软件发展的方向:CS–>BS–>SOA–>BIG DATA 建模的含义: 模型是对现实的简化 从特点视角对系统的一个完整性描述 建模的重要性: 为了更好的理解一个系统 管理复杂度(也 ...
- VS源码编译QuaZip(Windows下)
最近写个Qt demo,想要使用压缩和解压多个文件的功能,并不使用额外进程.网上参考了很多资料,发现只有QuaZip比较适合我的需求.但是QuaZip只提供源码,因此需要自己来编译. QuaZip简介 ...
- Python学习(一):编写购物车
1.购物车流程图: 2.代码实现: #!/usr/bin/env python #coding=utf-8 ChoiceOne =''' 1.查看余额 2.购物 3.退出 ''' ChoiceTwo ...
- RabbitMQ教程(一) ——win7下安装RabbitMQ
RabbitMQ依赖erlang,所以先安装erlang,然后再安装RabbitMQ; 下载RabbitMQ,下载地址: rabbitmq-server-3.5.6.exe和erlang,下载地址:o ...
- js构建函数优秀案例
这几个效果函数是看到别人写的,挺好的,复制下来学习备用! 函数封装: //var _hmt = _hmt || [];(function() {var hm = document.createElem ...
- Oracle导入导出常用命令
-- 全量导出 exp system/manager@TEST file=d:\daochu.dmp full=y -- 将数据库中system用户与sys用户的表导出 exp system/mana ...
- 关于linux命令ssh的总结
因为项目计算量比较大,需要将任务分布到多台电脑上面运行,因为对于分布式概念不熟,就想到了linux最简单的ssh协议,远程控制其他电脑,然后写shell脚本统一在所有电脑上运行程序.(我的操作系统为U ...
- 每天学一点Docker(4)-深入了解容器概念
什么是容器? 容器是一个自包含,可移植,轻量级的软件打包技术.是应用程序在任何地方几乎以相同方式运行.开发人员在开发机上创建好容器,无需任何修改就能在虚拟机,云服务器或公有云主机上运行. 容器与虚拟机 ...
- Python环境安装及编辑器配置(一)
在2018年决定写一些关于Python的文章,其实写博客这件事在2016年就有了,但是因为个人的一些原因一直被耽搁.所以2018年的目标之一就是写多一点的博客,不管是是生活还是工作.好吧,废话不多说, ...
- Django资源
官网地址:https://www.djangoproject.com/ 最新发布版本是:1.11.7 官网提供不同版本的文档:1.7.1.8.1.9.1.10.1.11.2.0.dev 安装不同的版本 ...