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): 58766 Accepted Submission(s): 15983
Problem Description
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
'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
Sample Input
Sample Output
Author
Source
Recommend
#include <iostream>
#include <cstring>
using namespace std;
char maze[][];
int isw[][];
int dx[]={,,,-};
int dy[]={,,-,};
int N,M,T;
int curx,cury,endx,endy;
int abs(int n)
{
return n>=?n:-n;
}
int dfs(int curx,int cury,int curt) //判断从当前位置(curx,cury)能否用刚好curt时间到达结束点
{
if(curt==){ //当时间耗尽的时候,判断是否到达了结束点
if(curx==endx && cury==endy)
return ;
else
return ;
}
//剪枝 1 :奇偶剪枝
int m = abs(curx-endx) + abs(cury-endy); //理想情况下,开始点到终点的最小步数
int t = curt; //要求走t步正好走到终点
//当 t<m 时,一定不能到达
//当 t>=m 时,要用t步从开始点正好走到终点,分两部分。
// 一部分为最小步数 m ,另一部分是为了凑够t步而余外多走的几步,设为 a。
// 而走出去就一定要走回来,所以走出去的步数和回来的步数一定是相等的,为b步。
// a=2b,所以多走的a步一定是偶数。
// 这里的奇偶剪枝就是判断 a 是否为偶数。如果不是偶数,一定不能到达。
if( t<m || (t-m)& )
return ;
for(int i=;i<;i++){ //剪枝 2
if( <=curx+dx[i] && curx+dx[i]<=N && <=cury+dy[i] && cury+dy[i]<=M //如果没有越界
&& !isw[curx+dx[i]][cury+dy[i]] //如果下一步没有走过
&& maze[curx+dx[i]][cury+dy[i]]!='X' ){ //如果下一步不是墙
isw[curx+dx[i]][cury+dy[i]]=true;
if(dfs(curx+dx[i],cury+dy[i],curt-)) //如果下一步这样走可以生存,则返回1
return ;
isw[curx+dx[i]][cury+dy[i]]=false;
}
}
return ;
} int main()
{
while(cin>>N>>M>>T){
if(N== && M== && T==) break;
memset(isw,,sizeof(isw)); //将isw[][]数组初始化为false,表示还没有走过。
for(int i=;i<=N;i++)
for(int j=;j<=M;j++){
cin>>maze[i][j];
if(maze[i][j]=='S'){ //记录开始点的位置
curx=i;
cury=j;
}
else if(maze[i][j]=='D'){ //记录终点的位置
endx=i;
endy=j;
}
}
isw[curx][cury]=true; //开始位置赋true
if(dfs(curx,cury,T)) //如果能够生存,输出YES,否则输出NO
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)的更多相关文章
- 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+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
随机推荐
- Virtualbox虚拟机安装Ubuntu图文版
这篇文章给大家介绍一下如何在Windows系统下的Virtual Box虚拟机软件中安装Ubuntu系统. 适用环境:Windows系统作为物理机,在此平台上搭建一个Virtual Box虚拟平台,在 ...
- mysql zip 版本配置方法
-\bin 指 C:\Program Files\MySQL\MySQL Server 5.6\bin 1.增加环境变量 "PATH"-"-\bin" 2.修改 ...
- Netbeans连接数据库
/* Netbeans连接数据库 NetBeans项目的“项目属性”中“库”一栏中.Tab页“编译和运行”中已经加上jdbc的驱动文件 */ Connection conn = null;//连接数据 ...
- ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇(转)
ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇 阅读目录 ASP.NET Identity 前世今生 建立 ASP.NET Identity 使用ASP.NET ...
- 保存知乎收藏夹功能的NodeJS版本
前两天发现知乎收藏夹中的答案正在不断减少..看来需要保存一下了,但之前别人的方式是用chrome插件(浏览器无法自动保存本地文件)+wget前后端配合来完成这个工作的,而且还有一些缺点(比如保存的ht ...
- 关于Java类加载双亲委派机制的思考(附一道面试题)
预定义类加载器和双亲委派机制 JVM预定义的三种类型类加载器: 启动(Bootstrap)类加载器:是用本地代码实现的类装入器,它负责将 <Java_Runtime_Home>/lib下面 ...
- PHP访问,增删改查,小结
PHP访问数据,增,删,改,查 增: 1,add.php 显示页面,利用 <form> 表单添加数据,数据添加到 name 中. 2,addchuli.php 处理页面,定义变量接受 $_ ...
- [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草
[BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草 试题描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草. 他知道N(1≤N≤100)个干草 ...
- [ZJOI3527][Zjoi2014]力
[ZJOI3527][Zjoi2014]力 试题描述 给出n个数qi,给出Fj的定义如下: 令Ei=Fi/qi.试求Ei. 输入 包含一个整数n,接下来n行每行输入一个数,第i行表示qi. 输出 有n ...
- nyoj19 全排列
http://acm.nyist.net/JudgeOnline/status.php?pid=19 #include<stdio.h> #include<stdlib.h> ...