HDOJ1010-Tempter of the Bone(搜索+奇偶剪枝)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ1010-Tempter of the Bone(搜索+奇偶剪枝)相关的知识,希望对你有一定的参考价值。
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.
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.
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
Means:
问你是否可以在第T秒恰好能从‘S’到‘D’点
Solve:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define CLR(x , v) memset(x , v , sizeof(x)) 5 6 static const int MAXN = 666; 7 static const int dirx[4] = {1 , 0 , -1 , 0}; 8 static const int diry[4] = {0 , 1 , 0 , -1}; 9 10 char data[MAXN][MAXN]; 11 int n , m , t; 12 int stx , edx , sty , edy; 13 bool flag; 14 bool vis[MAXN][MAXN]; 15 int sp; 16 17 void Dfs(int x ,int y , int ti) 18 { 19 if(x == edx && y == edy && ti == t) 20 { 21 flag = 1; 22 return ; 23 } 24 if(flag) 25 return; 26 for(int i = 0 ; i < 4 ; ++i) 27 { 28 int nx = x + dirx[i] , ny = y + diry[i]; 29 int tp = abs(edx - nx) + abs(edy - ny); 30 if(nx > n || ny > m || nx < 1 || ny < 1 || vis[nx][ny] || data[nx][ny] == ‘X‘) 31 continue; 32 if(ti + tp + 1 > t || (tp + ti + 1) % 2 != sp) 33 continue; 34 vis[nx][ny] = 1; 35 Dfs(nx , ny , ti + 1); 36 vis[nx][ny] = 0; 37 } 38 } 39 int main() 40 { 41 while(~scanf("%d%d%d" , &n , &m , &t) && (n || m || t)) 42 { 43 CLR(data , ‘\0‘); 44 CLR(vis , 0); 45 flag = 0; 46 for(int i = 1 ; i <= n ; ++i) 47 { 48 for(int j = 1 ; j <= m ; ++j) 49 { 50 scanf(" %c" , &data[i][j]); 51 if(data[i][j] == ‘S‘) 52 stx = i , sty = j; 53 if(data[i][j] == ‘D‘) 54 edx = i , edy = j; 55 } 56 } 57 58 sp = t & 1; 59 60 int dis = abs(stx - edx) + abs(sty - edy); 61 if(dis > t || dis % 2 != sp) 62 { 63 puts("NO"); 64 continue; 65 } 66 vis[stx][sty] = 1; 67 Dfs(stx , sty , 0); 68 69 if(flag) 70 puts("YES"); 71 else 72 puts("NO"); 73 } 74 }
以上是关于HDOJ1010-Tempter of the Bone(搜索+奇偶剪枝)的主要内容,如果未能解决你的问题,请参考以下文章
题解报告:hdu 1010 Tempter of the Bone