HDU - 1010 - Tempter of the Bone(dfs)
来源:互联网 发布:保研夏令营 知乎 编辑:程序博客网 时间:2024/06/10 13:25
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 106147 Accepted Submission(s): 28867
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
最开始以为T时间内就可以,结果一直T一直T一直T。。。后来发现是T时正好走到终点,剪枝后还是T,这就非常尴尬了orzzzzzzz
看了大神的博客,才知道还有奇偶剪枝这一条,好神奇。
奇偶剪枝:分析一下,发现从当前点到终点的最短步数,一定与还可以走多少步的奇偶性相同。感觉好神奇【懵】
#include <stdio.h>#include <math.h>#include <string.h>#include <algorithm>#include <queue>#include <iostream>#include <assert.h>#define INF 0x3f3f3f3fusing namespace std;char maps[10][10];int dir[5][2] = {1, 0, -1, 0, 0, -1, 0, 1};int n, m, T;int ans;int enx, eny;bool flag;bool judge(int x, int y){ if (x >= n || y >= m || x < 0 || y < 0) return false; return true;}void dfs(int x, int y, int cnt){ int xx, yy; if(flag) return; //找到一个答案就返回,无需寻找其他答案 if (cnt > T) return; if (x == enx && y == eny && cnt == T) { ans = min(ans, cnt); flag = true; return; } int mmt = abs(x - enx) + abs(y - eny); mmt = T - cnt - mmt; if (mmt & 1) return; //奇偶剪枝 for (int i = 0; i < 4; i++) { xx = x; yy = y; xx += dir[i][0]; yy += dir[i][1]; if(!judge(xx, yy)) continue; if(maps[xx][yy] == 'X') continue; if (maps[xx][yy] == '.') { maps[xx][yy] = 'X'; dfs(xx, yy, cnt + 1); maps[xx][yy] = '.'; } }}int main(){ while (cin >> n >> m >> T) { if(!n && !m && !T) break; ans = 0x3f3f3f3f; int stx, sty; flag = false; for (int i = 0; i < n; i++) { scanf("%s", maps[i]); for (int j = 0; j < m; j++) { if (maps[i][j] == 'S') { stx = i; sty = j; maps[i][j] = 'X'; } if (maps[i][j] == 'D') { enx = i; eny = j; maps[i][j] = '.'; } } } dfs(stx, sty, 0); if (ans > T) printf("NO\n"); else printf("YES\n"); } return 0;}
0 0
- HDU 1010 Tempter of the Bone (DFS)
- HDU 1010 Tempter of the Bone( DFS )
- HDU-1010 Tempter of the Bone (DFS)
- HDU 1010 Tempter of the Bone 【DFS】
- HDU 1010 Tempter of the Bone (DFS)
- hdu 1010 Tempter of the Bone(dfs)
- hdu 1010 Tempter of the Bone(DFS)
- dfs hdu 1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone[dfs]
- [HDU 1010] Tempter of the Bone DFS
- HDU 1010 Tempter of the Bone(DFS)
- HDU 1010 Tempter of the Bone(DFS)
- hdu 1010 Tempter of the Bone dfs
- hdu 1010Tempter of the Bone(DFS)
- HDU-1010-Tempter of the Bone【DFS】
- hdu-1010Tempter of the Bone-dfs
- hdu 1010 Tempter of the Bone (dfs)
- HDU 1010 Tempter of the Bone(dfs)
- 一个识别文件的工具
- jQuery EasyUI 教程
- jQuery 解析 JSON 数据
- spring 之浅析1
- @Resource 写错位置导致的错误
- HDU - 1010 - Tempter of the Bone(dfs)
- onethink的hook机制
- java.net.SocketException: Too many open files 问题的解决办法
- HTML站内搜索引擎
- iReport 下载地址
- R语言与机器学习学习笔记(分类算法)(5)神经网络
- Java常量
- Linux 网络命令详解
- 1082. Read Number in Chinese (25)