HDU 1010 Tempter of the Bone (广搜+减枝)
Problem Description
The 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
The 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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input`
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0`
Sample Output`
NO
YES`
分析:
迷宫中有一个出口,但是这个出口的门在最开始的时候是关着的,要求小狗必须在题目要求的第T秒到达这个出口的位置,此时这个门刚好打开且时间小于一秒,小狗刚好能出去。小狗只能够在上下左右移动,每当小狗到达一个位置的时候,这个地方地面就开始下沉并在下一秒消失,
值得注意的是题目上对于出去迷宫的时间的要求,必须在第T秒而不是在T秒内
然后就是这道题除了运用普通的广搜的思想外还应用到剪枝的方法。
补充下奇偶剪枝
把矩阵看成如下形式:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子 。
从为 1 的格子走一步,必然走向为 0 的格子 。
即:
从 0 走向 1 必然是奇数步,从 0 走向 0 必然是偶数步。
所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!
比如有一地图:
[c-sharp] view plaincopy
S...
....
....
....
...D
要求从S点到达D点,此时,从S到D的最短距离为s = abs ( dx - sx ) + abs ( dy - sy )。
如果地图中出现了不能经过的障碍物:
[c-sharp] view plaincopy
S..X
XX.X
...X
.XXX
...D
此时的最短距离s' = s + 4,为了绕开障碍,不管偏移几个点,偏移的距离都是最短距离s加上一个偶数距离。
就如同上面说的矩阵,要求你从0走到0,无论你怎么绕,永远都是最短距离(偶数步)加上某个偶数步;要求你从1走到0,永远只能是最短距离(奇数步)加上某个偶数步。
也就是说所要求的时间为t的话,起始点(sx,sy),终点(ex,ey)如果
t-[abs(ex-sx)+abs(ey-sy)] 结果为偶数,则可以在t步恰好到达;这是奇偶剪枝的主要应用。
下面具体说下这道题:
代码:
#include<stdio.h>
#include<math.h>
#include <algorithm>
#include<iostream>
using namespace std;
int n,m,t,sx,sy,ex,ey, op;
char tu[6][6]; ///存储图
int next1[4][2]= {{0,1},{1,0},{0,-1},{-1,0} };///便利四个方向
int bj[6][6]; ///标记当前的点是否被访问过
void dfs(int x,int y,int step)
{
if(op==1) ///op做一个标记,初始值设为0,如果op变为1的话就相当于已经到达出口
return ;
if(tu[x][y]=='D'&&step==t)///如果当前点为出口的话,标记op
{
op=1;
return ;
}
if( step>=t)///当前时间即步数(step) >= t 而且还没有找到D点
return ;
int mind=abs(x-ex)+abs(y-ey);
///注意到同奇或同偶相减结果为偶数,否则奇数。
///即剩余步数与最少步数如果奇偶性相同,那么它的差是偶数否则是奇数
int dis=t-step-mind;
if(dis<0||dis%2==1) /// 剩余步数小于最短距离或者与最少步数的奇偶性不同
///注 意到同奇或同偶相减结果为偶数,否则奇数。
return ;
for(int i=0; i<4; i++)
{
int tx=x+next1[i][0];
int ty=y+next1[i][1];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&tu[tx][ty]!='X'&&bj[tx][ty]==0)
{
bj[tx][ty]=1;
dfs(tx,ty,step+1);
bj[tx][ty]=0;
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t),n+m+t)
{
int k=0;
op=0;
for(int i=0; i<n; i++)
{
scanf("%s",tu[i]);
for(int j=0; j<m; j++)
{
bj[i][j]=0;
if(tu[i][j]=='S')
{
sx=i;
sy=j;
bj[i][j]=1;
}
if(tu[i][j]=='D')
{
ex=i;
ey=j;
}
if(tu[i][j]=='X')
{
k++;///k相当于找到图中的墙不能走的地方
}
}
}
if(n*m-k>t)///如果总共的地图格数减去墙剩余的地方《=t的话肯定不能到达
dfs(sx,sy,0);
if(op==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
HDU 1010 Tempter of the Bone (广搜+减枝)的更多相关文章
- 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(深搜+奇偶剪枝)
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经典题+奇偶剪枝详解】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- 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+奇偶剪枝)
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 + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- apache DBUtils 使用例子demo
转自:http://blog.csdn.net/earbao/article/details/44901061 apache DBUtils是java编程中的数据库操作实用工具,小巧简单实用, 1.对 ...
- Windows系统下搭建Appium自动化测试框架
简介 一种开源的测试框架(http://appium.io/) 能够用来测试原生Android/iOS应用.混合应用以及webapp 通过webdriver协议来操作应用,其核心是一个web服务器,接 ...
- linux grep --我最喜欢的命令~~
转:http://www.cnblogs.com/end/archive/2012/02/21/2360965.html Grep 命令 用法大全 1. 参数: -I :忽略大小写 -c :打印匹配的 ...
- Spring点滴二:Spring Bean
Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...
- Mybatis笔记八:MyBatis中#{}和${}的区别
先给大家介绍下MyBatis中#{}和${}的区别,具体介绍如下: 1. $将传入的数据直接显示生成在sql中 2. #方式能够很大程度防止sql注入. 3.$方式无法防止Sql注入. 4.$方式一般 ...
- C++模板编程中只特化模板类的一个成员函数
模板编程中如果要特化或偏特化(局部特化)一个类模板,需要特化该类模板的所有成员函数.类模板中大多数成员函数的功能可能是一模一样的,特化时我们可能只需要重新实现1.2个成员函数即可.在这种情况下,如果全 ...
- 【BZOJ4006】【JLOI2015】管道连接
Description 传送门 Solution 题目要求相同颜色的点必须在一个连通块中,但会有多个颜色同属一个连通块使得解更优的情况. 想一想DP能否行得通:设\(g_i\)表示已考虑颜色状态为\( ...
- 从function的定义看JavaScript的预加载
在JavaScript中定义一个函数,有两种写法: function ftn(){} // 第一种 var ftn = function(){} // 第二种 有人说,这两种写法是完全等价的.但是在解 ...
- web项目中的执行流程参数传递详解
还是从这个图开始讲解: struts2中有一个存放数据的中心:值栈.(值栈里面有map和对象栈) 首先:值栈的作用范围是一个请求:request作用域(一个请求是代表的一个过程,即页面点击到数据返回到 ...
- eos源码分析和应用(一)调试环境搭建
转载自 http://www.limerence2017.com/2018/09/02/eos1/#more eos基于区块链技术实现的开源引擎,开发人员可以基于该引擎开发DAPP(分布式应用).下面 ...