#include <iostream>
using namespace std;
int fgo[4][2] = {
0,1,
1,0,
0,-1,
-1,0,
};
int ok = 0;
char maze[10][10];
void fago( int n, int m, int x, int y, int dx,int dy,int t,int _t)
{
if (x == dx && y == dy && _t == t)
{
ok = 1;return;
}
if (x == dx && y == dy && _t != t)return;
if (ok == 1||_t>=t)return;
int tx, ty;
for (int i = 0;i < 4;i++)
{
tx = x + fgo[i][0], ty = y + fgo[i][1],_t++;
if (tx >= 0 && tx <= n - 1 && ty >= 0 && ty <= m - 1 && _t <= t)
{
if (maze[tx][ty] != 'X')
{
maze[tx][ty] = 'X';
fago( n, m, tx, ty, dx, dy, t, _t);
maze[tx][ty] = '.';
}
}
_t--;
}
}
int main()
{
int n, m, t;
while (cin >> n >> m >> t, n != 0 && m != 0 && t != 0)
{
int dx, dy, sx, sy;
for (int i = 0;i < n;i++)
{
for (int i1 = 0;i1 < m;i1++)
{
cin >> maze[i][i1];
if (maze[i][i1] == 'D')
dx = i, dy = i1;
if (maze[i][i1] == 'S')
sx = i, sy = i1;
}
}
int xx, yy;
xx = dx - sx;
if (xx < 0)
xx = -xx;
yy = dy - sy;
if (yy < 0)
yy = -yy;
if ((t - xx - yy) % 2 != 0)
cout << "NO" << endl;
else
{
int _t = 0;
maze[0][0] = 'X';
fago(n, m, sx, sy, dx, dy, t, _t);
if (ok == 1)
cout << "YES" << endl;
else cout << "NO" << endl;
}
ok = 0;
}
}
dfs+剪枝(搞了半天就是一种能通过起点终点的坐标和时间就能直接判断能否到达的操作),然后就没什么了。