DFS训练-Tempter of the Bone解题报告

Tempter of the Bone

题目描述:

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:

在这里插入图片描述

Sample Output:

在这里插入图片描述

题目描述:

描述有一条够一不小心进入迷宫,他要逃出去这个迷宫,必须进入这个迷宫的出口门,但这个门有个机关,你必须要在第T秒(重点这不是T秒前)到达这个门,如果可以就输出YES,不可以就输出NO

思路分析:

这道题就是简单的迷宫,但是必须要满足在第几步到达终点,只需要记录是否能达到要求步数就行了,直接暴力DFS去解决就可以了。

代码:

	#include<cstdio>
	#include<string.h>
	char b[8][8];//迷宫构造 
	int t[8][8];//标记是否走过 
	int flag;//标记是否走到终点 
	int x,y,T;//记录门开的时间和数组迷宫构造 
	int x1,y1,x2,y2;//记录终点和初始位置 
	int div[4][4]={{1,0},{0,1},{-1,0},{0,-1}};//四个方向移动 
	void LemonScanf()//得到迷宫构造 
	{
		int d,e;
		for(d=0;d<x;d++)
		{
			scanf("%s",b[d]);
	}
	for(d=0;d<x;d++)
	{
		for(e=0;e<y;e++)
		{
			if(b[d][e]=='S')//记录初始位置 
			{
				x1=d;
				y1=e;
			}
			if(b[d][e]=='D')//记录门(终点位置) 
			{
				x2=d;
				y2=e;
			}
		}
	}
	}
	void LemonDFS(int x1,int y1,int n)
	{
		int i;
		if(flag)//这里剪枝,缩短时间 
		return;
		if(x1==x2 && y1==y2)//如果走到终点,记录时间是否满足要求 
		{
			if(n==T)
			flag=1;
			return;
		}
		for(i=0;i<4;i++)
		{
			int x3=x1+div[i][0];
			int y3=y1+div[i][1];
			if(x3<0 || y3<0 || x3>=x || y3>=y || t[x3][y3] || b[x3][y3]=='X')//判断条件 
			continue;
			if(b[x3][y3]=='.' || b[x3][y3]=='D')//直接递归 
			{
				t[x3][y3]=1;
				LemonDFS(x3,y3,n+1);
				//回溯 
				t[x3][y3]=0;
			}
		}
		return;
	}
	int main()
	{
		
		while(scanf("%d %d %d",&x,&y,&T)!=EOF)
		{
			flag=0;
			if(x==0 && y==0 && T==0)break;
			memset(b,0,sizeof(b));
			memset(t,0,sizeof(t));
			LemonScanf();
			LemonDFS(x1,y1,0);
			if(!flag)
			printf("NO\n");
			else
			printf("YES\n");
		}
		return 0;
	}
发布了49 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xiaosuC/article/details/104242816
今日推荐