hdu 1010 Tempter of the Bone
Posted 琴影
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1010 Tempter of the Bone相关的知识,希望对你有一定的参考价值。
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 115817 Accepted Submission(s): 31439
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 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
分析:用深搜,题目只问能不能,所以需要立一个flag = 0,所有情况搜索完之后(有情况符合,则flag = 1),最后判断flag是否为1,不用奇偶剪枝的会超时(。。。奇偶剪枝现学,之前没听过,网上找了奇偶剪枝的原理)。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 char maze[10][10]; 8 bool visited[10][10]; 9 int N, M, T, sx, sy, ex, ey; 10 bool flag; 11 12 int dir_x[4] = {-1, 0, 1, 0}; 13 int dir_y[4] = {0, 1, 0, -1}; 14 15 void dfs(int i, int j, int cnt){ 16 if(flag == true) 17 return; 18 if((maze[i][j] == ‘D‘) && (cnt == T)){ 19 flag = true; 20 return; 21 } 22 if(cnt > T) 23 return; 24 int temp = T - cnt - abs(i - ex) - abs(j - ey);//奇偶剪枝的关键 25 if((temp < 0) || (temp & 1)) 26 return; 27 for(int k = 0; k < 4; k++){ 28 int dx = i + dir_x[k]; 29 int dy = j + dir_y[k]; 30 if((dx >= 0) && (dx < N) && (dy >= 0) && (dy < M) && (maze[dx][dy] != ‘X‘) && (!visited[dx][dy])) { 31 visited[dx][dy] = true; 32 dfs(dx, dy, cnt+1); 33 visited[dx][dy] = false; 34 } 35 } 36 return; 37 } 38 39 int main(){ 40 while(cin >> N >> M >> T){ 41 if(N == 0 && M == 0 && T == 0) 42 break; 43 int xnum = 0;//X即墙的数目 44 memset(visited, false, sizeof(visited)); 45 for(int i = 0; i < N; i++){ 46 for(int j = 0; j < M; j++){ 47 cin >> maze[i][j]; 48 if(maze[i][j] == ‘S‘){ 49 sx = i; 50 sy = j; 51 } 52 if(maze[i][j] == ‘D‘){ 53 ex = i; 54 ey = j; 55 } 56 if(maze[i][j] == ‘X‘){ 57 xnum++; 58 } 59 } 60 } 61 if((N * M - xnum) <= T){//可通过的格子数小于T,狗肯定没法活 62 cout << "NO" << endl; 63 continue; 64 } 65 flag = false; 66 visited[sx][sy] = true;//起点视为以访问过 67 dfs(sx, sy, 0); 68 if(flag == true) 69 cout << "YES" << endl; 70 else 71 cout << "NO" << endl; 72 } 73 return 0; 74 }
以上是关于hdu 1010 Tempter of the Bone的主要内容,如果未能解决你的问题,请参考以下文章
题解报告:hdu 1010 Tempter of the Bone
HDU 1010 Tempter of the Bone(dfs)