dfs:Tempter of the Bone

原创 2012年03月23日 15:08:05

Problem N

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 6

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

思路:这道题是讲小狗走不重复的路,问是否能按时到达出口的题目,既然是不重复的路,那么可以使用dfs的算法,当一个方向走不通的时候则换方向,如果所有路都走过则返回FALSE值。

代码实现:

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


DFS:Tempter of the Bone (规定时间达到规定地点)

解题心得: 1、注意审题,此题是在规定的时间达到规定的地点,不能早到也不能晚到。并不是最简单的dfs 2、在规定时间达到规定的地点有几个剪枝: 一、所需的步骤 - x相差行 - y相差列...

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 Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768...

HDU1010 Tempter of the Bone (DFS)

写的第二道DFS,这题在上ACM课的时候要求过,但是没好好去写,现在才第一次试着写,虽然还是要借鉴网上AC的代码,但是觉得自己对于如何写搜索有了一定的感受,这题的关键在于剪枝,奇偶性剪枝。 #incl...

【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 & %I64...

HDU 1010 Tempter of the Bone(dfs+奇偶剪枝)

http://acm.hdu.edu.cn/showproblem.php?pid=1010 AC代码: #include #include #include cha...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:dfs:Tempter of the Bone
举报原因:
原因补充:

(最多只允许输入30个字)