B - Tempter of the Bone(DFS+剪枝+回溯)

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解题,但如果直接使用DFS而不进行减枝代码优化,会时间超限,所以在BFS中使用减枝将不必要的分支减去

AC代码:

#include<iostream>
#include<cstdio> 
#include<cstring>
#include<cmath>
using namespace std;
int n,m,t,flag,sx,sy,dx,dy;
char maze[10][10];
int vis[10][10];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y,int num)
{
	if(flag==1)//已经确定可以存活,直接返回 
	return ;
	if(x==dx&&y==dy&&num==t)//找到符合条件,将flag标记为1
	{
		flag=1;
		return ;
	}
	//奇偶减枝将不符合条件的分支去掉,优化代码,减少时间复杂度 
	int minn=abs(x-dx)+abs(y-dy);//忽略墙的阻碍,从当前位置到终点所需的最短步数; 
	if(minn>t-num||(minn+t-num)%2!=0)
	//第一个条件为如果最短步数还要比当前剩余所需步数还要大说明不符合返回
	//第二个条件就是奇偶剪枝(详细介绍看下面的连接) 
	return ;
	for(int i=0;i<4;i++)
	{
		int a=x+dir[i][0];
		int b=y+dir[i][1];
		if(a>=0&&a<n&&b>=0&&b<m&&!vis[a][b]&&maze[a][b]!='X') 
		{
			vis[a][b]=1;
			dfs(a,b,num+1);
			 vis[a][b]=0;//回溯 
		}
	 } 
}
int main()
{
	int i,j,k;
	while(scanf("%d%d%d",&n,&m,&t)!=EOF,m+n+t)
	{
		k=0;
		for(i=0;i<n;i++)
		{
			scanf("%s",maze[i]);
			for(int j=0;j<m;j++)
			{
				if(maze[i][j]=='S')
				{
					sx=i;
					sy=j;
				}
				if(maze[i][j]=='D')
				{
					dx=i;
					dy=j;
				}
				if(maze[i][j]=='X')
				{
					k++;//记录墙的个数 
				}
			}
		}
		if(n*m-k-1<t)//n*m-k-1为doggie从开始位置到门口所需的最大时间,如果小于t,肯定不能存活 
		{
			printf("NO\n");
			continue;
		 } 
		 flag=0;//起标记作用
		 memset(vis,0,sizeof(vis));
		 vis[sx][sy]=1;
		 dfs(sx,sy,0);
		 if(flag)
		 printf("YES\n");
		 else
		 printf("NO\n"); 
	}
	return 0;
} 

 奇偶剪枝介绍请看连接https://baike.baidu.com/item/%E5%A5%87%E5%81%B6%E5%89%AA%E6%9E%9D

剪枝算法详细介绍请看连接https://blog.csdn.net/qq_37763204/article/details/79516313 

猜你喜欢

转载自blog.csdn.net/qq_40707370/article/details/81349528