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.<br><br>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.<br>
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:<br><br>'X': a block of wall, which the doggie cannot enter; <br>'S': the start point of the doggie; <br>'D': the Door; or<br>'.': an empty block.<br><br>The input is terminated with three 0's. This test case is not to be processed.<br>
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.<br>
Sample Input
4 4 5<br>S.X.<br>..X.<br>..XD<br>....<br>3 4 5<br>S.X.<br>..X.<br>...D<br>0 0 0<br>
Sample Output
NO<br>YES<br>
-.-小狗在迷宫里找门口,-.-还必须是在规定时间门才会打开,机智的小狗如何从迷宫中跑出
-.-不是最短路径,那就用深搜吧,-.-题目虽然好想,要命的是怎么样都是超时-.-,在网上看别人的发现要剪枝,受教了!
最大的收获就是学习剪枝。
神奇的奇偶剪枝。
奇偶剪枝:根据题目,doggie必须在第t秒到达门口。也就是需要走t-1步。设doggie开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+abs(ey-y)为偶数,则abs(ex-x)和abs(ey-y)奇偶性相同,所以需要走偶数步;
当abs(ex-x)+abs(ey-y)为奇数时,则abs(ex-x)和abs(ey-y)奇偶性不同,到达目标位置就需要走奇数步。先判断奇偶性再搜索可以节省很多时间,不然的话容易超时。t-sum为到达目标位置还需要多少步。因为题目要求doggie必须在第t秒到达门的位置,所以(t-step)和abs(ex-x)+abs(ey-y)的奇偶性必然相同。因此temp=(t-step)-abs(ex-x)+abs(ey-y)必然为偶数。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int m, n, t, ok, bx, by, ex, ey;
char map[6][6];
int vis[6][6];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
void dfs(int x, int y, int step)
{
if(ok == 1)
return;
if(step == t)
{
if(x == ex && y == ey)
ok = 1;
return;
}
int dis = abs(x - ex) + abs(y - ey);
if((dis > t - step) || ((dis + t - step) % 2 != 0))
return;
for(int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx <= m-1 && ny >= 0 && ny <= n-1 && vis[nx][ny] == 0 && map[nx][ny] != 'X')
{
vis[nx][ny] = 1;
dfs(nx, ny, step + 1);
vis[nx][ny] = 0;
}
}
}
int main()
{
while(scanf("%d%d%d", &m, &n, &t) && (m || n || t))
{
int num = 0;
for(int i = 0; i < m; i++)
{
scanf("%s", map[i]);
for(int j = 0; j < n; j++)
{
if(map[i][j] == 'X')
num++;
if(map[i][j] == 'S')
{
bx = i; by = j;
}
if(map[i][j] == 'D')
{
ex = i; ey = j;
}
}
}
if(n * m - num - 1 < t)
{
printf("NO\n");
continue;
}
ok = 0;
memset(vis, 0, sizeof(vis));
vis[bx][by] = 1;
dfs(bx, by, 0);
if(ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int m, n, t, ok, bx, by, ex, ey;
char map[6][6];
int vis[6][6];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
void dfs(int x, int y, int step)
{
if(ok == 1)
return;
if(step == t)
{
if(x == ex && y == ey)
ok = 1;
return;
}
int dis = abs(x - ex) + abs(y - ey);
if((dis > t - step) || ((dis + t - step) % 2 != 0))
return;
for(int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx <= m-1 && ny >= 0 && ny <= n-1 && vis[nx][ny] == 0 && map[nx][ny] != 'X')
{
vis[nx][ny] = 1;
dfs(nx, ny, step + 1);
vis[nx][ny] = 0;
}
}
}
int main()
{
while(scanf("%d%d%d", &m, &n, &t) && (m || n || t))
{
int num = 0;
for(int i = 0; i < m; i++)
{
scanf("%s", map[i]);
for(int j = 0; j < n; j++)
{
if(map[i][j] == 'X')
num++;
if(map[i][j] == 'S')
{
bx = i; by = j;
}
if(map[i][j] == 'D')
{
ex = i; ey = j;
}
}
}
if(n * m - num - 1 < t)
{
printf("NO\n");
continue;
}
ok = 0;
memset(vis, 0, sizeof(vis));
vis[bx][by] = 1;
dfs(bx, by, 0);
if(ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}