Tempter of the Bone

本文介绍了一种使用深度优先搜索(DFS)解决迷宫逃脱问题的方法,通过合理的剪枝策略优化搜索过程,确保在限定时间内找到出口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tempter of the Bone

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

题目答疑:有一只狗要吃骨头,结果进入了一个迷宫陷阱,迷宫里每走过一个地板费时一秒,该地板 就会在下一秒塌陷,所以你不能在该地板上逗留。迷宫里面有一个门,只能在特定的某一秒才能打开,让狗逃出去。现在题目告诉你迷宫的大小和门打开的时间,问你狗可不可以逃出去,可以就输出YES,否则NO。

利用DFS深度遍历看小狗能不能在指定时间恰好到达门口。

在这之前首先介绍一下奇偶剪枝

什么是奇偶剪枝?

把矩阵看成如下形式: (x+y为奇数则为1,偶数为0)

0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
从为 0 的格子走一步,必然走向为 1 的格子 。
从为 1 的格子走一步,必然走向为 0 的格子 。
即: 
从 0 走向 1 必然是奇数步,从 0 走向 0 必然是偶数步。

所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!

比如有一地图:

S...
....
....
....
...D

要求从S点到达D点,此时,从S到D的最短距离为s = abs ( dx - sx ) + abs ( dy - sy )。

如果包含了障碍物:

S..X
XX.X
...X
.XXX
...D

此时的最短距离s' = s + 4,为了绕开障碍,不管偏移几个点,偏移的距离都是最短距离s加上一个偶数距离。

就如同上面说的矩阵,要求你从0走到0,无论你怎么绕,永远都是最短距离(偶数步)加上某个偶数步;要求你从1走到0,永远只能是最短距离(奇数步)加上某个偶数步。


遍历过程中需要用到剪枝:

1.如果当前时间即步数(step) >= T 而且还没有找到D点,则剪掉。

2.设当前位置(x, y)到D点(dx, dy)的最短距离为s,到达当前位置(x, y)已经花费时间(步数)step,那么,如果题目要求的时间T - step < s,则剪掉。

3. 对于当前位置(x, y),如果,(T-step-s)是奇数,则剪掉(奇偶剪枝)。因为T-step为剩余步数,s为最短距离,假如最后能到达出口,则剩余步数与最短距离一定符合同奇同偶原则,所以两者的差一定为偶数,如果为奇数则肯定不能到达。

4.如果地图中,可走的点的数目(xnum) < 要求的时间T,则剪掉(路径剪枝)。

#include<iostream>
#include<cmath>
using namespace std;
char metric[7][7];
int startX,startY,endX,endY, N,M,T,xnum,dir[4][4]= {{0,1},{1,0},{-1,0},{0,-1}};
bool solved,visited[7][7];
int Distance(int x,int y) {
	return abs(x-endX)+abs(y-endY);//当前点到终点的最短距离
}
void DFS(int x,int y,int step) {
	if(solved) return;
	if(metric[x][y]=='D'&&step==T) {
		solved=true;
		return ;
	}
	if(step>=T) { //当前步数已经超过T并还没找到门
		return ;
	}
	int dis=T-step-Distance(x,y);//剩余时间(T-step)和最短距离(Distance)必须是同奇同偶的,
//所以dis必须是偶数才行,否则剪枝就可以了。
	if(dis<0||dis%2) return ;
	for(int i=0; i<4; i++) {
		int nextX=x+dir[i][0];
		int nextY=y+dir[i][1];
		if(nextX>=0&&nextX<N&&nextY>=0&&nextY<M&&metric[nextX][nextY]!='X'&&!visited[nextX][nextY]) {
			visited[nextX][nextY]=true;
			DFS(nextX,nextY,step+1);
			visited[nextX][nextY]=false;
		}
	}
}
int main() {
	while(cin>>N>>M>>T,N+M+T) {
		solved=false;//判断小狗是否能得救
		xnum=0;//记录墙的数量
		for(int i=0; i<N; i++) {
			for(int j=0; j<M; j++) {
				visited[i][j]=false;
				cin>>metric[i][j];
				if(metric[i][j]=='S') {
					startX=i;
					startY=j;
					visited[i][j]=true;
				} else if(metric[i][j]=='D') {
					endX=i;
					endY=j;
				} else if(metric[i][j]=='X') {
					xnum++;
				}
			}
		}
		if(N*M-xnum>T) { //如果可行点大于要求的步数,进行深度优先搜索,其中包括剪枝
			DFS(startX,startY,0);
		}
		if(solved) {
			cout<<"YES"<<endl;
		} else {
			cout<<"NO"<<endl;
		}
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值