题目链接:hdu 1010
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
走迷宫,熟悉的.路X墙,S开始D结束,然而:
1.走过的路会毁坏,不能再走;
2.多出个T,要求在第T秒正好到达D.
这样看就很明显是一道dfs的题了。同时剪枝也很重要(不太懂剪枝这门高深的学问):
1.S到D的曼哈顿距离不能大于T,这个很好理解;
2.S到D的曼哈顿距离和T的奇偶性要相等,这个自己稍微模拟一下也是可以理解的,数学证明这个我是真不会……
3.m * n - wall 必须大于 T,wall即墙的数量,这个也不难理解,在去掉墙和起点之后,可以走的总步数就剩下了m * n - 1 - wall,因此想要有解,m * n - 1 - wall (即剩余的最大可走步数)不能小于 T,即m * n - wall > T.
第三个剪枝我写的效果是从561ms→78ms,另外两个我没有试过,据discuss众题友们说,第二个是可以防T的。
PS:一开始看错题,直接写了bfs,习惯性的把变量t定义成了全局变量,改dfs的时候为了回溯本来应该改成局部变量的,没注意到结果WA哭。找WA找到一堆剪枝我……我也是给自己跪了Orz,跑来整理题解。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#define M 20
using namespace std;
struct node
{
int x, y;
}st, ed;
int n, m, T, wall, to[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char g[M][M];
bool vis[M][M], flag;
void dfs(int depth, node tmp)
{
if(tmp.x == ed.x && tmp.y == ed.y && !flag)
{
if(!depth)
{
printf("YES\n");
flag = true;
}
return;
}
if(flag)
return;
for(int i = 0; i < 4; i++)
{
node t = tmp;
t.x += to[i][0];
t.y += to[i][1];
if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && !vis[t.x][t.y] && g[t.x][t.y] != 'X')
{
vis[t.x][t.y] = true;
dfs(depth - 1, t);
vis[t.x][t.y] = false;
}
}
}
int main()
{
while(scanf("%d %d %d", &n, &m, &T) && n + m + T)
{
wall = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> g[i][j];
if(g[i][j] == 'S')
st.x = i, st.y = j;
else if(g[i][j] == 'D')
ed.x = i, ed.y = j;
else if(g[i][j] == 'X')
wall++;
}
}
if(((int)(abs(ed.x - st.x) + abs(ed.y - st.y)) % 2) ^ (T % 2) || abs(ed.x - st.x) + abs(ed.y - st.y) > T || n * m - wall <= T)//剪枝 * 3
{
printf("NO\n");
continue;
}
flag = false;
memset(vis, false, sizeof(vis));
vis[st.x][st.y] = true;
dfs(T, st);
if(!flag)
printf("NO\n");
}
return 0;
}
运行结果: