#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int x, y, t, si, sj, ei, ej, wall;
int dir[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
char map[11][11];
bool flag;
bool in(int x0, int y0){ return x0 < x&&x0 >= 0 && y0 < y&&y0 >= 0; }
void dfs(int x0, int y0, int temp)
{
if (flag)
return;
if (temp == t)
{
if (x0 == ei&&y0 == ej&&t == temp)
flag = 1;
return;
}
for (int i = 0; i < 4; i++)
{
int xx = x0 + dir[i][0], yy = y0 + dir[i][1];
if (in(xx, yy) && map[xx][yy] == '.')
{
map[xx][yy] = 'X';
dfs(xx, yy, temp + 1);
map[xx][yy] = '.';
}
}
}
int main()
{
while (cin >> x >> y >> t, x | y | t)
{
wall = 0;
flag = 0;
for (int i = 0; i < x; i++)
{
cin >> map[i];
for (int j = 0; j < y; j++)
{
if (map[i][j] == 'S')
si = i, sj = j, map[i][j] = 'X';
if (map[i][j] == 'D')
ei = i, ej = j, map[i][j] = '.';
if (map[i][j] == 'X')
wall++;
}
}
if (x*y - wall < t)
{
cout << "NO" << endl;
continue;
}
if ((abs(si - ei) + abs(sj - ej)) % 2 != t % 2)
{
puts("NO");
continue;
}
else
dfs(si, sj, 0);
if (flag)puts("YES");
else puts("NO");
}
}
HDOJ 1010 DFS 170MS
最新推荐文章于 2018-11-08 11:07:56 发布