HDU1010-Tempter of the Bone(DFS模板,例题)
2014-08-07 21:06
483 查看
原博客链接:http://www.cnblogs.com/Dreamcaihao/archive/2013/05/25/3099216.html
Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49637 Accepted Submission(s):
13320
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
[align=left]Sample Input[/align]
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
[align=left]Sample Output[/align]
NO
YES
HDU 1010 Tempter of the Bone(DFS)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) MemoryLimit: 65536/32768 K (Java/Others)
Total Submission(s): 49637 Accepted Submission(s):
13320
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
[align=left]Sample Input[/align]
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
[align=left]Sample Output[/align]
NO
YES
#include <stdio.h> #include <string.h> #include <math.h> bool flag; int si, sj; int ei, ej; int N, M, T; char maze[10][10]; int dir[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}}; void dfs(int curX, int curY, int curT) { if(flag)return ; //如果找到出口,直接返回 if(curT > T)return ; //如果当前花费的时间大于T,就返回 //花费的时间加上剩余需要的时间若大于T,返回 if(curT + abs(ei-curX) + abs(ej-curY) > T)return ; //若到达出口最短距离和剩余时间的奇偶性不同,返回 if( (abs(ei-curX) + abs(ej-curY))%2 != abs(T-curT)%2 )return; if(curX == ei && curY == ej && curT == T)//找到出口 { printf("YES\n"); flag = true; return ; } for(int k = 0; k < 4; k++) { int nextX = curX + dir[k][0]; int nextY = curY + dir[k][1]; if(nextX >= 0 && nextX < N && nextY >= 0 && nextY < M)//在迷宫的范围内 { if(maze[nextX][nextY] != 'X') { maze[nextX][nextY] = 'X'; //使其不可达到 dfs(nextX, nextY, curT + 1); maze[nextX][nextY] = '.'; //回溯时,恢复原来的状态 } } } } int main() { while(scanf("%d %d %d", &N, &M, &T) && M+N+T) { int k = 0; getchar(); flag = false; memset(maze, 0, sizeof(maze)); for(int i = 0; i < N; i++) { for(int j = 0; j < M; j++) { scanf("%c", &maze[i][j]); if(maze[i][j] == 'S') { si = i; sj = j; maze[i][j] = 'X'; //将开始位置置为不可达状态 } if(maze[i][j] == 'D') { ei = i; ej = j; } if(maze[i][j] != 'X') k++; } getchar(); } if(k < T) //若可走的点数小于T,则输出NO { printf("NO\n"); continue; } dfs(si, sj, 0); if(!flag) printf("NO\n"); } return 0; }
相关文章推荐
- HDU1010_Tempter of the Bone(DFS+奇偶剪枝+回溯)
- HDU1010:Tempter of the Bone(DFS)
- hdu1010 Tempter of the Bone dfs搜索 剪枝
- HDU1010 Tempter of the Bone(搜索,dfs)
- HDU1010 Tempter of the Bone(深度优先搜索DFS+奇偶性剪枝)
- HDU1010 Tempter of the Bone (DFS)
- HDU1010 Tempter of the Bone 【DFS】+【剪枝】
- Tempter of the Bone(hdu1010,dfs)
- hdu1010 Tempter of the Bone --DFS & 奇偶剪枝
- HDU1010 Tempter of the Bone (DFS & 奇偶剪枝)
- hdu1010 Tempter of the Bone(dfs)
- hdu1010 Tempter of the Bone (DFS)
- DFS+剪枝 hdu1010 Tempter of the Bone
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
- 【搜索-DFS】hdu1010 Tempter of the Bone
- hdu1010 Tempter of the Bone(DFS)
- hdu1010 Tempter of the Bone(dfs+奇偶剪枝)
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
- HDU1010 Tempter of the Bone DFS
- 【hdu1010】Tempter of the Bone——dfs