hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 131057    Accepted Submission(s): 35308
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.
'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.
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
YES
代码如下:
#include<stdio.h>//hdu1010 dfs+奇偶性剪枝
#include<stdlib.h> char map[][];
int n,m,t, wall, si, sj, di, dj;
int d[][] = {{, }, {, -}, {, }, {-, }}; int dfs(int i, int j, int step)
{
if(step == t)//如果最后一秒
{ //判断是否到达door
if(i == di && j == dj) return ;
else return ;
} if (i == di && j == dj )//如果到达door
{
//判断是否最后一秒
if (step == t) return ;
else return ;
} //奇偶性剪枝
int temp = t-step-abs(i - di) + abs(j - dj);
if( temp% )
return ; for (int k = ; k<; k++)
{
int x = i + d[k][];
int y = j + d[k][];
if (x>= && x<=n && y>= && y<=m && map[x][y] != 'X')
{
map[x][y] = 'X';
if(dfs(x, y, step + )) return ;
map[x][y] = '.';//回溯出口,记得复原(可能把'D'变成'.',但已经用didj保存其位置,所以没关系)
}
}
return ;
} int main()
{
while(scanf("%d%d%d",&n,&m,&t) &&m &&n &&t)
{
wall = ;
for (int i = ; i<=n; i++)
{
scanf("%s",map[i]+);
for (int j = ; j<=m; j++)
{
//用scanf(%c) 就出问题?
if (map[i][j] == 'S') { si = i; sj = j; }
if (map[i][j] == 'D') { di = i; dj = j;}
if (map[i][j] == 'X') { wall++; }
}
} map[si][sj] = 'X';
if (n * m - wall- < t)
{ //初始判断剩余的空格(记得减去初始位置)是否够走
puts("NO");
continue;
} if (dfs(si, sj, ))
puts("YES");
else
puts("NO");
}
return ;
}
hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝的更多相关文章
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
		http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ... 
- HDU1010:Tempter of the Bone(dfs+剪枝)
		http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ... 
- 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 ... 
- Tempter of the Bone     搜索---奇偶性剪枝
		Tempter of the Bone Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ... 
- M - Tempter of the Bone(DFS,奇偶剪枝)
		M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ... 
- 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+奇偶剪枝)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ... 
- hdu1010 Tempter of the Bone(深搜+剪枝问题)
		Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ... 
- hdu 1010 Tempter of the Bone (奇偶性剪枝)
		题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其 ... 
随机推荐
- Flex 布局教程学习
			转载自:阮一峰的网络日志(http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html) 网页布局(layout)是 CSS 的一个重点应用. 布局 ... 
- Spring 让 LOB 数据操作变得简单易行,LOB 代表大对象数据,包括 BLOB 和 CLOB 两种类型
			转自:https://www.ibm.com/developerworks/cn/java/j-lo-spring-lob/index.html 概述 LOB 代表大对象数据,包括 BLOB 和 CL ... 
- C 标准库 - <time.h>
			C 标准库 - <time.h> 简介 time.h 头文件定义了四个变量类型.两个宏和各种操作日期和时间的函数. 库变量 下面是头文件 time.h 中定义的变量类型: 序号 变量 &a ... 
- react 通过 classnames 处理 多个class 的问题
			react原生动态添加多个className会报错: import style from './style.css' <div className={style.class1 style.cla ... 
- vs2012 MinGW 编译ffmeg 引用外部库libx264,librtmp
			VS2012如何编译ffmpeg前面已经有文章讲过,本来主要讲述如何引用外部库libx264,librtmp, ffmpeg版本是3.0.2. 1. 下载x264源文件并编译 源码地址是http:// ... 
- Git bare repo with multiple branches
			http://stackoverflow.com/questions/9324762/git-bare-repo-with-multiple-branches Q: I want to make a ... 
- 技术总结--android篇(三)--代码规格和编码规范
			命名规则 变量名: 1)尽量要取有意义的名字,比方说:一个用户名的成员变量.应该写成username.而不要仅仅写个string: 2)假设是常量.既在编码过程中.这个值是不会改变的,应该写成大写的名 ... 
- leetcode第一刷_Symmetric Tree
			必须承认,一開始这道题我是不会做的.由于我心目中的树遍历仅仅能用一个节点发起.多么天真而无知. 我想不通如何同一时候遍历两颗子树.由于根节点一定是一个啊.但是,作为对称轴上的它.从一開始就不应该被考虑 ... 
- 实现iOS7上tableView的切割线像iOS6中的效果
			iOS7上tableView的切割线左边短了一点,要实现和iOS6中的效果还是有方法的,UITableView头文件中个属性: @property (nonatomic) UIEdge ... 
- 基于flask做权限控制
			和Django实现的原理类似,有时间补充 
