//
// main.cpp
// Richard
//
// Created by 邵金杰 on 16/9/24.
// Copyright © 2016年 邵金杰. All rights reserved.
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10+5;
char map[maxn][maxn];
int vis[maxn][maxn];
int n,m,T;
int sx,sy,ex,ey;
bool ok;
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int abs(int x){return x>0?-x:x;}
bool inside(int x,int y)
{
return x<1||x>n||y<1||y>m;
}
void dfs(int x,int y,int t)
{
if(ok) return ;
if(x==ex&&y==ey&&t==0) {ok=true;return ;}
if(t<=0) return ;
int temp=t-abs(ex-x)-abs(ey-y);
if(temp<0||temp%2!=0) return ;
for(int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(inside(nx,ny)) continue;
if(map[nx][ny]=='X') continue;
if(vis[nx][ny]==1) continue;
vis[nx][ny]=1;
dfs(nx,ny,t-1);
vis[nx][ny]=0;
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&T)!=EOF)
{
if(n==0&&m==0&&T==0) break;
int tot=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
scanf("%s",map[i]+1);
for(int j=1;j<=m;j++){
if(map[i][j]=='S') sx=i,sy=j;
if(map[i][j]=='D') ex=i,ey=j;
if(map[i][j]=='X') tot++;
}
}
if(n*m-tot-1<T) {printf("NO\n");continue;}
if((T-abs(ex-sx)-abs(ey-sy))%2==1) {printf("NO\n");continue;}
ok=false;
memset(vis,0,sizeof(vis));
vis[sx][sy]=1;
dfs(sx,sy,T);
if(ok) printf("YES\n");
else printf("NO\n");
}
return 0;
}
HDU 1010 Tempter of the Bone(dfs+奇偶剪枝)
最新推荐文章于 2017-01-24 12:15:00 发布