关闭

dfs:Tempter of the Bone

91人阅读 评论(0) 收藏 举报

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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4605次
    • 积分:124
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档