Tempter of the Bone(迷宫&&DFS)

题目出处(点击)
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
题意:
题目告诉我们一个N*M的矩阵,S为起点,D为终点, ‘ . ’为可走的路,X为墙不可走的路,有一点的要求就是给定时间准时到达。
思路:
一下子就想到的是简单暴力直接DFS,但是这要做会超时的,所以不能简单地直接DFS,需要进行一些剪枝。
主要的剪枝条件有:
1、剩余可走区域小于时间
2、奇偶性剪枝
3、越界
4、超时等
其中最重要的就是奇偶性剪枝:
下面主要说说奇偶性剪枝
若有一迷宫,将迷宫的每一个位置有0或1表示(x+y为偶数时 为0 否则为1):
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
从图中我们可以很清晰的看出:任意一个位置周围相邻的必然是与本身值相反的值,也就是说,要想走到与本身值相同的点必然要走偶数步;
同理,要想走到与本身相异的值的点必然要走奇数步;因为需要准时到达,所以,当两个位置的奇偶性相同时(同为0或同为1)若时间为奇数 则必然无法到达当两个位置的奇偶性不同时(一个为1,另一个为0)若时间为偶数也必不能到达
直接上代码,有注释:

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

int n,m,t;
char house[9][9];		//一个数组,此题它也可以当标记数组
int starti,startj,endi,endj;
int flag;		//初值默认为 0 
int direction[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};		//便于四个方向的DFS
void DFS(int px,int py,int time)
{
	int i;
	if(flag == 1)return;//找到了 
	if(house[px][py] == 'X')return;	//碰壁
	if(px==endi&&py==endj&&time==t)
	{
		flag = 1;		//到了终点
		return;
	}
	if(px < 1||px > n||py < 1||py > m)return;//越界 
	if(time > t)return; 	//超时
	for(i = 0; i < 4; i++)
	{
		house[px][py] = 'X';
		DFS(px+direction[i][0],py+direction[i][1],time+1);
		house[px][py] = '.';	//回溯
		if(flag == 1)return;	//此题要求就是准时,只要符合条件直接退出,其他迷宫求花费时间最少,那此处就不能有这个
	} 
}

int main()
{
	int i,j,block;
	while(cin >> n >> m >> t&&n&&m&&t)
	{
		block = 0;
		flag = 0;
		for(i = 1; i <= n;i++)
		{
			for(j = 1; j <= m; j++)
			{
				cin >> house[i][j];
				if(house[i][j] == 'S'){
					starti = i;
					startj = j;
					continue;
				}
				if(house[i][j] == 'X'){
					block++;	//计算一共有多少墙 ,利于下面剪枝
					continue;
				}
				if(house[i][j] == 'D'){
					endi = i;
					endj = j;
				}
			} 
		}
		//剪枝 
		if(t>(n*m-block))	//时间大于最大范围,舍掉 
		{
			cout << "NO" << endl;
			continue;
		}
		if((endi+starti+startj+endj+t)%2==1) 
		{
          cout<<"NO"<<endl;
          continue;
		}
		//搜索
		DFS(starti,startj,0);
		if(flag==1)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;	
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_46687179/article/details/105623071