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 ...
随机推荐
- jQuery对表单元素的取值和赋值操作代码(转)
使用常规的思路:$("#keyword").value 取值是取不到的,因为此时$('#keydord')已经不是个element,而是个jquery对象,所以应该使用:$(&qu ...
- iOS: FFmpeg编译和使用 学习
ffmpeg是一个多平台多媒体处理工具,处理视频和音频的功能非常强大.目前在网上搜到的iOS上使用FFMPEG的资料都比较陈旧,而FFMPEG更新迭代比较快: 且网上的讲解不够详细,对于初次接触FFM ...
- IDS 源镜像端口添加
把核心交换机的G1/2口镜像到目的交换机的G1/4口,两个交换机之间都是连接的24口 1.核心交换机配置 Ruijie# configure tRuijie(config)# vlan 77Ruiji ...
- bzoj 3996: [TJOI2015]线性代数
Description 给出一个N*N的矩阵B和一个1*N的矩阵C.求出一个1*N的01矩阵A.使得 D=(A*B-C)*A^T最大.其中A^T为A的转置.输出D Input 第一行输入一个整数N,接 ...
- Oracle学习笔记_09_字符串相关函数
二.参考资料 0.Oracle中的字符串类型及相关函数详解 1.ORACLE 字符串操作 2.oracle函数大全-字符串处理函数
- HBase资料
http://blog.csdn.net/ymh198816/article/details/51244911 https://www.cnblogs.com/JingJ/p/4521245.html ...
- ASP.NET如何通过后台数据库提供的链接播放视频(不使用外置插件)
1.后台视频数据库: 2.aspx中的关键代码: <asp:DataList ID="DataList2" runat="server" DataKeyF ...
- JavaScript Array 对象方法 以及 如何区分javascript中的toString()、toLocaleString()、valueOf()方法
1.concat() 2.join() 3.pop() 4.push() 5.reverse() 6.shift() 7.unshift() 8.slice() 9.sort() 10.splice( ...
- 图解JQUERY尺寸及位置定义
最近在学习JQUERY的一些应用,接触到了JQUERY对于元素尺寸及位置定义,还有就是配合浏览器尺 寸及状态的计算所做出的一些动画特效.其实像这类JQUERY应用无外乎涉及这些属性的调用:innerH ...
- Odwiedziny[POI 2015]
题目描述 给定一棵n个点的树,树上每条边的长度都为1,第i个点的权值为a[i]. Byteasar想要走遍这整棵树,他会按照某个1到n的全排列b走n-1次,第i次他会从b[i]点走到b[i+1]点,并 ...