#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define LL long long
using namespace std;
const int MAXN = 10;
char s[MAXN][MAXN];
int N, M, T;
int sx, sy, ex, ey;
int dir[4][2] = {{-1,0},{1,0}, {0,1},{0,-1}};
int ok;
void dfs(int x, int y, int cnt)
{
if(cnt == T)
{
if(x == ex && y == ey)
ok = 1;
return ;
}
if(ok) return ;
int dis = abs(x - ex) + abs(y - ey) - abs(T - cnt);
if(dis > 0 || dis & 1) return ;
for(int i=0;i<4;i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(nx >= 0 && nx < N && ny >= 0 && ny < M && s[nx][ny] != 'X' )
{
s[nx][ny] = 'X';
dfs(nx, ny, cnt+1);
s[nx][ny] = '.';
}
}
}
int main()
{
while(scanf("%d%d%d", &N, &M, &T)!=EOF)
{
if(N == 0 && M == 0 && T == 0)
break;
int wall = 0;
for(int i=0;i<N;i++)
{
scanf("%s", s[i]);
for(int j=0;j<M;j++)
{
if(s[i][j] == 'S')
{
sx = i;
sy = j;
}
if(s[i][j] == 'D')
{
ex = i;
ey = j;
}
if(s[i][j] == 'X')
wall++;
}
}
if(N * M - wall <= T)
{
printf("NO\n");
continue;
}
s[sx][sy] = 'X';
ok = 0;
dfs(sx, sy, 0);
if(ok) printf("YES\n");
else printf("NO\n");
}
return 0;
}
HDU 1010 Tempter of the Bone(dfs + 奇偶性剪枝)
最新推荐文章于 2021-07-21 20:42:17 发布