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

### Sample Input

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;
}



• 本文已收录于以下专栏：

## C语言DFS(5)____Tempter of the Bone(HDU 1010)

Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. Howev...

## HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O...

## HDU 1010 Tempter of the Bone(DFS,奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...

## 【HDU 1010 Tempter of the Bone】 + DFS + 减枝

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other...

## HDU 1010.Tempter of the Bone【DFS+奇偶性剪枝】9月19

Tempter of the Bone Problem Description The doggie found a bone in an ancient maze, which fa...

## hdu 1010 Tempter of the Bone dfs+奇偶剪枝

H - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &...

## HDU 1010 Tempter of the Bone（dfs+奇偶剪枝）

http://acm.hdu.edu.cn/showproblem.php?pid=1010 AC代码： #include #include #include cha...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)