HDU-OJ-1010 Tempter of the Bone
来源:互联网 发布:热血传奇蛮王数据 编辑:程序博客网 时间:2024/06/06 01:53
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
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.
'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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES
Author
ZHANG, Zheng
————————————————————剪枝后的分割线
思路:这一题是对DFS的初步提升。DFS是一种暴力的搜索,耗时令人无法忍受。但是如果善于剪枝,像本题时限从1s改成100ms也是能A的!所谓剪枝,就是一种预判。题目中暗示了很多隐藏条件。利用这些条件避免无谓的搜索,可以大量节省时间。
首先分析隐藏条件。门只在某一时刻开启。(条件1!)走过的地方会塌陷。(条件2!)
我们模拟一下会发现,某一时刻非常重要!时刻是偶数的话,起点与终点之间隐藏的关系是,从某一起点走到某一终点,步数的奇偶性是固定不变的。用图表示如下:
利用这一点进行奇偶性剪枝可以剪掉一些本身就不可能有解的数据。然后,每走一步都判断一下,剩下的时间是否甚至不足以在最短路径下抵达出口。又剪掉大部分。最重要的是,成功逃脱从之后,不必再DFS,一步一步return true。
代码如下:
————————————————————剪枝后的分割线
思路:这一题是对DFS的初步提升。DFS是一种暴力的搜索,耗时令人无法忍受。但是如果善于剪枝,像本题时限从1s改成100ms也是能A的!所谓剪枝,就是一种预判。题目中暗示了很多隐藏条件。利用这些条件避免无谓的搜索,可以大量节省时间。
首先分析隐藏条件。门只在某一时刻开启。(条件1!)走过的地方会塌陷。(条件2!)
我们模拟一下会发现,某一时刻非常重要!时刻是偶数的话,起点与终点之间隐藏的关系是,从某一起点走到某一终点,步数的奇偶性是固定不变的。用图表示如下:
0 1 0 1 01 0 1 0 10 1 S 1 01 0 1 0 10 1 0 1 0S是起点,0表示走到该点需要偶数步。1表示奇数步。
利用这一点进行奇偶性剪枝可以剪掉一些本身就不可能有解的数据。然后,每走一步都判断一下,剩下的时间是否甚至不足以在最短路径下抵达出口。又剪掉大部分。最重要的是,成功逃脱从之后,不必再DFS,一步一步return true。
代码如下:
#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#define LIM nx >= 0 && nx < l && ny >= 0 && ny < rint sx, sy, ex, ey;int l, r, time;int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};char mat[10][10];//不必将地图转换成数字储存bool dfs(int x, int y, int dist) { //bool型dfs,只为了剪枝~ if(x == ex&&y == ey&&dist == time) return true;//首先判断是否走到终点且时间一致 if(abs(ex - x) + abs(ey - y) > time - dist) return false;//剩余时间根本不够!剪枝 for(int d = 0; d < 4; d++) {//接下来就要探索四个方向 int nx = x + dx[d], ny = y + dy[d]; if(mat[nx][ny]!='X' && LIM && dist+1 <= time) {//能走,未超地图,下一步未超时,注意等于号! mat[nx][ny] = 'X';//走了这一点,变成‘X’ if(dfs(nx, ny, dist+1)) return true;//进入下一层,并且一旦返回了真,本层也返回真,一路返回,全部剪枝 mat[nx][ny] = '.';//没能返回真,该点当作没走过。 } } return false;//四个方向都不行,false}int main() { while(scanf("%d%d%d", &l, &r, &time), l||r||time) { int step = 0;//用step统计能走的点个数,以备剪枝 for(int i = 0; i < l; i++) { scanf("%s", mat[i]); for(int j = 0; j < r; j++){ switch(mat[i][j]) { case 'S':sx = i; sy = j;break; case 'D':ex = i; ey = j;step++;break;//最后一点也能走! case '.':step++;break; } } } int road = abs(ex - sx) + abs(ey - sy);//横纵坐标差值之和 if(step < time || (time + road)%2==1) puts("NO");//当可走的点小于时限或者坐标差值跟时限奇偶性不同,不可能生还 else { mat[sx][sy] = 'X';//起点一定要记得设成已走过 if (dfs(sx, sy, 0)) puts("YES"); else puts("NO"); } }return 0;}
0 0
- HDU-OJ-1010 Tempter of the Bone
- HDU OJ 1010 Tempter of the Bone【深搜】
- HDU 1010 Tempter of the Bone
- 迷宫 hdu 1010 Tempter of the Bone
- HDU 1010-Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone(深搜)
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU-1010-Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010-Tempter of the Bone
- HDU 1010 Tempter of the Bone (DFS)
- hdu 1010 Tempter of the Bone
- Android开发之自定义字体
- Java引用与C++引用的区别
- Pat(Advanced Level)Practice--1058(A+B in Hogwarts)
- 九度题目1201:二叉排序树
- 数组中,有三个数只出现1次,其他都出现2次,找出来那三个数。 3 Unique
- HDU-OJ-1010 Tempter of the Bone
- 2004-2014还有多少个十年
- Quatz 定时任务
- 边缘检测(2)Roberts算子
- 【拷贝】Eclipse中删除了tomcat后
- putty如何连接到虚拟机下的Linux平台
- HDU 1021Fibonacci Again
- 从KMP算法,trie树再到AC自动机
- Bellman-ford算法学习与SPFA算法