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的,只不过非常麻烦,并且内存消耗极大,故而不推荐使用
本文不过多阐述如何进行奇偶剪枝,因为其他博客已经写的很多了。
本文阐述一下这道题目的误区:BFS.
首先,毋庸置疑,DFS是可以解决本题的,因为数据小,并且水。
然后,第一眼看这个题,我觉得一个是用BFS,但是交了很多次都错了。
为什么?因为BFS会有多个分支,并且每个分支都会更新vis。讲道理,正常的BFS这样是正确的,而这题就不一样了,因为它会绕路。绕路会导致其他支线走过的地方就不能绕了。BFS的初衷是最短路(大概可以这样讲吧),所以平时的vis是用与判断找到过的就不去了。这里因为要绕路,被其他支线走了,但你还要绕就只能每个节点记下全图的vis了,所以,空间消耗非常大。
如果一定想要试试,我觉得BFS+奇偶剪枝+状态压缩(或者hash)应该是可行的。
然后BFS有一个case
4 4 5
.XSX
.X.X
…X
..DX
大家可以测试一下
[代码]
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
char s[10][10];
int ax, ay, bx, by, n, m, TTime;
int dire[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int vis[10][10];
int flag;
void dfs(int x, int y, int cnt)
{
if (x == bx&&y == by)
{
if (TTime == cnt)flag = 1;
return;
}
if (cnt >= TTime)
return;
if (s[x][y] == 'X')return;
for (int i = 0; i<4; i++)
{
int nowx = x + dire[i][0];
int nowy = y + dire[i][1];
if (s[nowx][nowy] != 'X'&&nowx >= 1 && nowx <= n&&nowy >= 1 && nowy <= m && !vis[nowx][nowy])
{
vis[nowx][nowy] = 1;
dfs(nowx, nowy, cnt + 1);
vis[nowx][nowy] = 0;
if (flag)
return;
}
}
}
int main()
{
while (scanf("%d%d%d", &n, &m, &TTime)>0)
{
if (!n && !m && !TTime)return 0;
int i, count;
for (i = 1; i <= n; i++)
{
getchar();
for (int j = 1; j <= m; j++)
{
scanf("%c", &s[i][j]);
if (s[i][j] == 'S')
{
ax = i;
ay = j;
}
if (s[i][j] == 'D')
{
bx = i;
by = j;
}
}
}
getchar();
memset(vis, 0, sizeof(vis));
if (abs(ax - bx) + abs(ay - by) > TTime || (ax + bx + ay + by) % 2 != TTime % 2)
{
printf("NO\n");
continue;
}
flag = 0;
count = 0;
vis[ax][ay] = 1;
dfs(ax, ay, count);
if (flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}