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


暑假的时候,小明和朋友去迷宫中寻宝。然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱!他们想尽一切办法逃出去。
迷宫是一个大小为 N*M 的长方形,迷宫中有一扇门。一开始,门是关着的,他会在第 t 秒的时间打开。因为,小明和朋友必须在第 t 秒到大门口。每一秒,他都可以向上下左右四个方向移动一个点。一旦他移动了,他刚才所在的点就消失,(这意味着他不能回到他已经走过的点)。他不能在一个点上停留超过一秒,并且不能走障碍点。小明和朋友能安全的逃出吗?
输入描述
输入由多个测试用例组成。每个测试用例的第一行包含三个整数 N、M 和 T ( 1 < N , M < 7 ; 0 < T < 50 ),分别表示迷宫的大小和门打开的时间。接下来的N行给出迷宫布局,每一行包含M个字符。下列字母分别表示:
“X”: 一堵墙,小明和朋友不能在上面停留
“S”: 起点
“D”: 门
“.”: 可以走的点
输入以 3 个 0 时结束。这个测试用例不需要处理。
输出描述
对于每组样例输出一行。
如果小明能够安全逃出,输出 “YES” ,否则输出 “NO”。
 

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<algorithm>
#define SIS std::ios::sync_with_stdio(false)
#define ll long long
#define INF 0x3f3f3f3f
#define mod 998244353
const int MAXN = 1e9 + 7;
using namespace std;
const int N = 1e5 + 5;
/*int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}
ll _power(int a, int b)//计算a^b;
{
	int ans=1, res=a;
	while(b){
		if(b&1) ans=ans*res%mod;
		res=res*res%mod;
		b>>=1;
	}
	return ans%mod;
}*/
int dis[4][2] = { -1,0,1,0,0,1,0,-1 };
char map1[50][50];
int vis[50][50];
int n, m,t, ex, ey, sign=0;//到达位置
void dfs(int sx,int sy,int step) {//step为步骤数
	int nextx, nexty;
	if (sign) return;
	if (sx == ex && sy == ey && step == t) {
		sign = 1;
		return;
	}
	int tem = t - step - abs(ex - sx) - abs(ey - sy);//剪枝代码
	if (tem < 0 || tem%2==1)//剪枝:如果剩余的步数已经不足以走到出口,且必须是偶数,偶数-偶数=偶数,奇数-奇数=偶数
		return;
	for (int i = 0; i < 4; i++) {
		nextx = sx + dis[i][0];
		nexty = sy + dis[i][1];
		if (nextx < 0 || nexty >= m || nexty < 0 || nextx >= n) continue;
		if (map1[nextx][nexty] != 'X' && vis[nextx][nexty] == 0) {
			vis[nextx][nexty] = 1;//及时标记,先判断后标记,先确定能不能走 ,在考虑走完标记的问题
			dfs(nextx, nexty, step + 1);
			if (sign)
				return;
			vis[nextx][nexty] = 0;
		}
	}
	return;
}
int main()
{
	SIS;
	int sx, sy;//初始位置
	int wall = 0;
	while (~scanf("%d %d %d",&n,&m,&t) ){
		if (n ==0&& m ==0&& t==0) break;
		sign = 0, wall = 0;
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; i++)
			scanf("%s", map1[i]);
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				if (map1[i][j] == 'S') {
					sx = i;
					sy = j;
				}
				if (map1[i][j] == 'D') {
					ex = i;
					ey = j;
				}
				if (map1[i][j] == 'X') {
					wall++;
				}
			}
		if (t > n * m - wall - 1) {//注意时间与墙数和地图的数量关系会减少很多步骤哦
			printf("NO\n");
			continue;
		}
		vis[sx][sy] = 1;
		dfs(sx, sy, 0);
		if (sign) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

注意奇偶性剪枝

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值