1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include<bits/stdc++.h> using namespace std; const int maxn = 10; const int dy[4] = {1,0,-1,0}; const int dx[4] = {0,1,0,-1}; int a[maxn][maxn],n,m,t,c,si,sj,ok,ei,ej; char gtc() { int c = getchar(); while(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\b')c=getchar(); return char(c); } void dfs(int x,int y,int d) { if(ok||x<1||y<1||x>n||y>m||d>t)return; #ifdef DEBUG printf("Visit dfs(%d,%d,%d) %d \n",x,y,d,a[x][y]); #endif if(a[x][y]==3) { #ifdef DEBUG printf("Visising D but d is %d \n",d); #endif if(d==t)ok=1; return; } for(int i=0;i<4;i++) { int xx=x+dx[i],yy=y+dy[i]; if(a[xx][yy]==1||x<1||y<1||x>n||y>m)continue; if(a[xx][yy]!=3)a[xx][yy]=1; dfs(xx,yy,d+1); if(a[xx][yy]!=3)a[xx][yy]=0; } } int main(void) { while(scanf("%d%d%d",&n,&m,&t) == 3) { memset(a,0,sizeof a); if(n == 0 && m == 0 && t == 0)return 0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { c=gtc(); switch(c) { case 'X': a[i][j]=1; break; case 'S': a[i][j]=1; si=i;sj=j; break; case 'D': a[i][j]=3; ei=i;ej=j; break; default: break; } } ok=0; #ifdef DEBUG for(int i=1;i<=n;i++,puts("")) for(int j=1;j<=m;j++) cout<<a[i][j]; #endif int path = abs(si-ei)+abs(sj-ej); if(t<path||(t-path)&1) puts("NO"); else { dfs(si,sj,0); if(ok)puts("YES"); else puts("NO"); } } return 0; }
|