题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
//C++代码
#include<iostream>
#include<cstdlib>
using namespace std;
int n,m,t,dx,dy;
char maze[8][8];
const int xx[]={-1,0,1,0};
const int yy[]={0,1,0,-1};
bool dfs(int x,int y,int cnt){
if(cnt==t && x==dx && y==dy) return 1;
if(x==0 || x==n+1 || y==0 || y==m+1) return 0;
int k=(t-cnt)-abs(x-dx)-abs(y-dy);
if(k<0 || k&1) return 0;
for(int i=0;i<4;i++){
int tx=x+xx[i],ty=y+yy[i];
if(maze[tx][ty]!='X'){
maze[tx][ty]='X';
if(dfs(tx,ty,cnt+1)) return 1;
maze[tx][ty]='.';
}
}
return 0;
}
int main(){
while(cin>>n>>m>>t,n||m||t){
int i,j,sx,sy,num=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>maze[i][j];
if(maze[i][j]=='S') sx=i,sy=j;
else if(maze[i][j]=='D') dx=i,dy=j;
else if(maze[i][j]=='X') num++;
}
}
if(n*m-num<=t) cout<<"NO"<<endl;
else{
maze[sx][sy]='X';
if(dfs(sx,sy,0)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}