hdu1010 Tempter of the Bone (深搜)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<stdlib.h> char map[10][10]; int T,M,N,sx,sy,ex,ey; int ans; int dir[4][2]={-1,0,1,0,0,1,0,-1}; bool Input(){ scanf("%d%d%d\n",&N,&M,&T); if(!N && !M && !T) return false; int i,j; for(i=1;i<=N;i++){ gets(map[i]+1); for(j=1;j<=M;j++){ if(map[i][j]=='S'){ sx=i; sy=j; } else if(map[i][j]=='D'){ ex=i; ey=j; } } } return true; } void dfs(int x,int y,int k){ if(ans) return; if(k==T){ if(map[x][y]=='D') ans=1; return; } int i,a,b; char ch=map[x][y]; map[x][y]='X'; for(i=0;i<4;i++){ a=x+dir[i][0]; b=y+dir[i][1]; if(a>0 && a<=N && b>0 && b<=M && (map[a][b]=='.' || map[a][b]=='D')){ dfs(a,b,k+1); } } map[x][y]=ch; } int main(){ //freopen("in.txt","r",stdin); memset(map,0,sizeof(map)); while(Input()){ if((abs(ex-sx)+abs(ey-sy))%2 != T%2){ puts("NO"); continue; } ans=0; dfs(sx,sy,0); puts(ans?"YES":"NO"); memset(map,0,sizeof(map)); } return 0; }