HDUOJ-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): 155243 Accepted Submission(s): 41368
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
思路:DFS+剪枝
代码:
#include<iostream> #include<string.h> #include<math.h> using namespace std; #define NUM 7 char maze[NUM][NUM]; int vis[NUM][NUM]; int Time,M,N; int start_x, start_y, end_x, end_y; int flag; int go[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };//定义上下左右四个方向数组 void dfs(int x, int y, int Step) { if (x == end_x&&y == end_y&&Step == Time) { flag = 1; return; } int temp = Time - Step - abs(end_x - x) - abs(end_y - y);//过程中奇偶剪枝的核心代码 if (temp < 0 || temp & 1) //这就是属于提前判断,看看剩下的步数还能不能到,或者符不符合奇偶 return; for (int i = 0; i < 4; i++) { int nx = x + go[i][0]; int ny = y + go[i][1]; if (nx<1 || nx>M || ny<1||ny > N || maze[nx][ny] == 'X' || vis[nx][ny] == 1) { continue; } vis[nx][ny] = 1;//走过这条路就不能走了 dfs(nx, ny,Step+1); //这是刚才那只狗的结果反馈之后,我们应该怎样做 if (flag)//一种那只狗已经找到了那个点 return;//我们这些人就没有必要再往下找了 //要是刚才那只狗没有找到合适的路径,我们就不能占着这个点了,我们需要放出这个位置 vis[nx][ny] = 0; //可能下一只狗能用到这个点,一旦回来之后这些点都会被释放 } } int main() { int i, j; while (cin >> M >> N >> Time) { memset(vis, 0, sizeof(vis)); memset(maze, 0, sizeof(maze)); if (!M&&!N&&!Time) break; int wall = 0; flag = 0; for (i = 1; i <= M; i++) { for (j = 1; j <= N; j++) { cin >> maze[i][j]; if (maze[i][j] == 'S') { start_x = i; start_y = j; } else if (maze[i][j] == 'D') { end_x = i; end_y = j; } else if (maze[i][j] == 'X') { wall++; } } } if (Time > M*N - wall - 1) { cout << "NO" << endl; continue; } int temp = abs(end_x - start_x) + abs(end_y - start_y); if ((temp + Time) & 1) { cout << "NO" << endl; continue; } vis[start_x][start_y] = 1; dfs(start_x, start_y,0); if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
以大多数人努力程度之低,根本轮不到去拼天赋~