# Tempter of the Bone

**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 54205 Accepted Submission(s): 14593
**

Problem Description

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately
to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively.
The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;

'S': the start point of the doggie;

'D': the Door; or

'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

'X': a block of wall, which the doggie cannot enter;

'S': the start point of the doggie;

'D': the Door; or

'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

Sample Input

4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0

Sample Output

NO YES

DFS+奇偶剪枝：

1.先按照原来的DFS来写，最后加上奇偶剪枝的条件

#include<stdio.h> #include<string.h> #include<stdlib.h> char a[9][9]; bool vis[9][9],flag; int m,n,e_x,e_y; void DFS(int x,int y,int step) { if(flag || x<0 || x>=n || y<0 || y>=m ) return;//已到达终点后面不需再判断 if(step==0 && a[x][y]=='D') { flag=1; return;}//到达终点 if(step< abs(x-e_x)+abs(y-e_y) || (step-(abs(x-e_x)+abs(y-e_y))&1)) return ;//奇偶剪枝 if(!vis[x+1][y] && a[x+1][y]!='X') { vis[x+1][y]=1; DFS(x+1,y,step-1); vis[x+1][y]=0; } if(!vis[x][y+1] && a[x][y+1]!='X') { vis[x][y+1]=1; DFS(x,y+1,step-1); vis[x][y+1]=0; } if(!vis[x-1][y] && a[x-1][y]!='X') { vis[x-1][y]=1; DFS(x-1,y,step-1); vis[x-1][y]=0;} if(!vis[x][y-1] && a[x][y-1]!='X') { vis[x][y-1]=1; DFS(x,y-1,step-1); vis[x][y-1]=0;} } int main() { int t,i,j,s_x,s_y; while(scanf("%d%d%d",&n,&m,&t),!(n==0&&m==0&&t==0)) { getchar();//吸收回车 for(i=0;i<n;i++){ for(j=0;j<m;j++){ a[i][j] = getchar(); if(a[i][j]=='S'){ s_x = i; s_y = j;}//起点位置 if(a[i][j]=='D'){ e_x = i; e_y = j;} //终点位置 } getchar();//吸收回车 } flag=0; memset(vis,0,sizeof(vis)); vis[s_x][s_y]=1; //!!!!!!!!!起点置1 ，这一点在开始忘了

DFS(s_x,s_y,t); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }