#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
#define N 8
using namespace std;
int row;
int col;
int step;
int sx,sy,ex,ey;
bool can;
char maze[N+1][N+1];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void DFS(int x,int y,int cnt)
{
if(can)
return ;
if(x==ex&&y==ey&&cnt == step)
{
can = true;
return ;
}
int tmp = abs(ex-x)+abs(ey-y)-abs(cnt-step);
if(tmp>0||tmp%2)
return ;
for(int i=0;i<4;i++)
{
int nx = x+dir[i][0];
int ny = y+dir[i][1];
if(nx>=0&&nx<row&&ny>=0&&ny<col&&maze[nx][ny]=='.')
{
maze[nx][ny] = 'X';
DFS(nx,ny,cnt+1);
maze[nx][ny] = '.';
}
}
}
int main()
{
while(scanf("%d%d%d",&row,&col,&step)!=EOF&&(row||col||step))
{
can = false;
memset(maze,0,sizeof(maze));
for(int i=0;i<row;i++)
scanf("%s",maze[i]);
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
if(maze[i][j]=='S')
{
sx = i;
sy = j;
maze[i][j] = '.';
}
if(maze[i][j]=='D')
{
ex = i;
ey = j;
maze[i][j] = '.';
}
}
maze[sx][sy] = 'X';
DFS(sx,sy,0);
if(can)
printf("YES\n");
else
printf("NO\n");
}
}
hdu1010 Tempter of the Bone
最新推荐文章于 2022-12-04 15:41:50 发布