# Problem N

### 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.

### 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.

### Output

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

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

### Sample Output

NO
YES

#include <stdio.h>
#include <string.h>
struct queuenod{
int x,y;
}que[100000];

int count, sx, sy, dx, dy, tx, ty, wx, wy, head = 0,tail = 0;			//定义
int a, b, n, t, temp, change;
int dir[4][2] = { {1,0} , {0,1} , {-1,0} , {0,-1} };
char map[50][10];
bool result;
int m, i, j,exist;
int inarea(int x,int y)					//判断是否在区域内
{
return x >= 0 && y >= 0 && x < a && y < b;
}

bool dfs(int c)
{
int k;
int x1,y1;
if (que[n].x == dx && que[n].y == dy && n == t) return true;	//到终点则退出
for (k = 0;k < 4;k ++){											//四个方向
tx = que[n].x + dir[k][0];
ty = que[n].y + dir[k][1];
if (inarea(tx,ty) && map[tx][ty] == 0){						//如果在区域内又路可走，则进入dfs递归
que[n+1].x = tx;
que[n+1].y = ty;
map[tx][ty] = 1;
n++;
result = dfs(n);
n = change;
if (result == 1) return true;							//退出条件
}
}
map[que[n].x][que[n].y] = 0;
n--;
change = n;
return false;
}

int main(void)									//主函数
{
while(scanf("%d%d%d",&a,&b,&t)==3&&a!=0){
memset(map, 1, sizeof(map));
while(scanf(" ")||scanf("\n")) ;
for(i = 0;i < a; i++){					//扫描地图
for(j = 0;j < b; j++){
scanf("%c",&temp);
if (temp == 'X') map[i][j] = 1;
else if (temp == '.') map[i][j] = 0;
else if (temp == 'S') {
map[i][j] = 0;
sx = i;
sy = j;
}
else if (temp == 'D') {
map[i][j] = 0;
dx = i;
dy = j;
}
else {
j--;
continue;
}
}
}
n=0;												//初始化数据
que[0].x = sx;
que[0].y = sy;
map[sx][sy] = 1;
result = 0;
result = dfs(0);
if (result == 1) printf("YES\n");					//输出
else printf("No\n");
}
return 0;
}

06-29 1182

04-26 3188

07-03 1745

10-30 1447

09-17 1936

07-23 712

08-02 807

08-11 608

09-16 572

08-06 3595