#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
using namespace std;
int n, m, t;
int flag;
int sx, sy, dx, dy;
char maze[8][8];
int vis[8][8];
int mv[4][2] = {{0, 1},{1, 0},{-1, 0}, {0, -1}};
void dfs(int x, int y, int time)
{
if(x == dx && y == dy)
{
if(time == t)
flag = 1;
}
else
{
if(flag == 1)
return;
int i;
for(i = 0; i < 4; i++)
{
int tx = x + mv[i][0];
int ty = y + mv[i][1];
if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty] && (maze[tx][ty] == '.' || maze[tx][ty] == 'D'))
{
vis[tx][ty] = 1;
dfs(tx, ty, time+1);
vis[tx][ty] = 0;
}
}
}
}
int main()
{
int i, j;
while(cin >> n >> m >> t)
{
if(!n && !m && !t)
break;
flag = 0;
for(i = 0; i < n; i++)
{
cin >> maze[i];
for(j = 0; j < m; j++)
{
if(maze[i][j] == 'S')
{
sx = i; sy = j;
}
if(maze[i][j] == 'D')
{
dx = i; dy = j;
}
}
}
memset(vis, 0, sizeof(vis));
vis[sx][sy] = 1;
dfs(sx, sy, 0);
if(flag == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
Tempter of the Bone(HDU 深搜)
最新推荐文章于 2021-12-03 22:43:55 发布