题意:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
doggie在起点'S'的时候地板开始下陷,他必须马上移动,'X'是墙,不能通过,'.'是地板,可以通过,但是一走过地板就开始下陷,必须刚好在T时间到达'D'大门,再能逃生。
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;
int direction[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
void wall(string* maze, int N, int M)
{
maze[N+1] = maze[0] = string(M+2, 'X');
for (int i = 1; i <= N; ++i)
maze[i] = 'X' + maze[i] + 'X';
}
void getXY(string* maze, int N, int M, int& x, int& y, char c)
{
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j)
{
if (maze[i][j] == c)
{
x = i;
y = j;
return;
}
}
}
bool dfs(string* maze, bool(*visited)[9], int x, int y, int T)
{
if (!T)
{
if (maze[x][y] == 'D')
return true;
return false;
}
visited[x][y] = true;
for (int i = 0; i < 4; ++i)
{
int X = x + direction[i][0], Y = y + direction[i][1];
if (visited[X][Y] == false && maze[X][Y] != 'X')
if (dfs(maze, visited, X, Y, T-1))
return true;
}
visited[x][y] = false;
return false;
}
bool result(string* maze, int N, int M, int T)
{
wall(maze, N, M);
int x, y, ex, ey;
getXY(maze, N, M, x, y, 'S');
getXY(maze, N, M, ex, ey, 'D');
int min_step = abs(ex-x) + abs(ey-y);
if (T < min_step || ((T - min_step) & 1))
return false;
bool visited[9][9];
memset(visited, false, sizeof(visited));
return dfs(maze, visited, x, y, T);
}
int main()
{
int N, M, T;
int i;
string maze[9];
while (cin >> N >> M >> T, N || M || T)
{
while (getchar() != '\n');
for (i = 1; i <= N; ++i)
getline(cin, maze[i]);
cout << (result(maze, N, M, T) ? "YES" : "NO") << endl;
}
}