HDU 1010 - Tempter of the Bone

http://acm.hdu.edu.cn/showproblem.php?pid=1010

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28956    Accepted Submission(s): 7893


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
 

这道题是让我纠结时间比较长的一道搜索题~~一眼看上去是迷宫问题,很像bfs,但是写完后无论怎么改都是wa~~后来才看出来这道题求的是是否存在从出发点到终点距离为time的路径,而bfs求的是最短路径,因此这里用bfs不可能对~~(我的看法,百度上有人说bfs出来了,不理解。。。真有的话--拜大神~~)我将bfs改为dfs加回溯,又开始超时,各种剪枝之后才ac,其中有输入地图时判断能走的方块个数判断是否存在为time的路径,判断出发点和目的地之间的最短距离是否小于time,以及奇偶性剪枝等等~~~最郁闷的是我敲代码时太快,把一个y敲成x了,然后一直错误,盯着看了半晌没看出来,为此纠结复纠结~~~Orz~~~总之,这道题终于a了,学到了不少~~

代码如下:

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 using namespace std;
6 int m, n, t, ok, bx, by, ex, ey;
7 char map[6][6];
8 int vis[6][6];
9 int dx[] = {-1, 0, 1, 0};
10 int dy[] = {0, -1, 0, 1};
11 void dfs(int x, int y, int step)
12 {
13 if(ok == 1)
14 return;
15 if(step == t)
16 {
17 if(x == ex && y == ey)
18 ok = 1;
19 return;
20 }
21 int dis = abs(x - ex) + abs(y - ey);
22 if((dis > t - step) || ((dis + t - step) % 2 != 0))
23 return;
24 for(int i = 0; i < 4; i++)
25 {
26 int nx = x + dx[i];
27 int ny = y + dy[i];
28 if(nx >= 0 && nx <= m-1 && ny >= 0 && ny <= n-1 && vis[nx][ny] == 0 && map[nx][ny] != 'X')
29 {
30 vis[nx][ny] = 1;
31 dfs(nx, ny, step + 1);
32 vis[nx][ny] = 0;
33 }
34 }
35 }
36 int main()
37 {
38 while(scanf("%d%d%d", &m, &n, &t) && (m || n || t))
39 {
40 int num = 0;
41 for(int i = 0; i < m; i++)
42 {
43 scanf("%s", map[i]);
44 for(int j = 0; j < n; j++)
45 {
46 if(map[i][j] == 'X')
47 num++;
48 if(map[i][j] == 'S')
49 {
50 bx = i; by = j;
51 }
52 if(map[i][j] == 'D')
53 {
54 ex = i; ey = j;
55 }
56 }
57 }
58 if(n * m - num - 1 < t)
59 {
60 printf("NO\n");
61 continue;
62 }
63 ok = 0;
64 memset(vis, 0, sizeof(vis));
65 vis[bx][by] = 1;
66 dfs(bx, by, 0);
67 if(ok)
68 printf("YES\n");
69 else
70 printf("NO\n");
71 }
72 return 0;
73 }

 

你可能感兴趣的:(HDU)