HDU 1010 Tempter of the Bone 骨头的诱惑

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

一只小狗受骨头的诱惑进入了一个迷宫,初始时刻为0,小狗每走一步时间+1,迷宫的门只有在t时刻的时候打开一下,问小狗能不能走出迷宫(t时刻刚好到达迷宫的门)。‘S’表示小狗初始位置,‘D’表示门到的位置,‘.’表示空地(可行走),‘X’表示墙(不能行走)。每个位置只能行走一次。

这题还是用DFS做,但要转个弯。
刚开始直接做超时了三四次,然后去百度,学到了一招奇偶性剪枝,也就是我说的那个弯。
我这里简单说一下奇偶性剪枝。
地图上某点坐标为(x,y),如果x+y为奇数,这个点就是奇数点,x+y为偶数,这个点就是偶数点。
地图上奇偶点总是交替存在的,如下图,我用0表示偶数点,1表示奇数点:
0 1 0 1 
1 0 1 0
0 1 0 1
1 0 1 0

如果小狗从偶数点出发,时间t就相当于它走的步数,如果走偶数步的话,只能到达偶数点。这时候如果门在奇数点,小狗就永远不能刚好在t时刻停在门。这时候就没必要进行DFS,进行了一次剪枝。在其他的奇偶位置也同理。
 
 
然后是代码:
 
 
#include<stdio.h>  
#include<string.h>  
#include<iostream>  
using namespace std;
#define MAX 1005
char mpt[MAX][MAX];
int vis[MAX][MAX];
int a,b,time,ans;//ans用于标记是否确定能出去
int turn[4][2] = { 0, -1, 0, 1,  1, 0, -1,0};
void DFS(int x,int y,int t)
{
	if (ans)return;
	if (t == 0){
		if (mpt[x][y] == 'D')//t剩余0,并且当前刚好在D上,ans=1,能出去
		{
			ans = 1;
		}
			return;//不管能不能出去,此时t已经等于0了,门只打开一下,继续走以及没用了,所以直接返回
	}
	int ty, tx;
	int i;
	for (i = 0; i < 4; i++){
		if (ans)return;
		tx = x+ turn[i][0];
		ty = y + turn[i][1];
		if (tx < 0 || ty < 0 || tx >= a || ty >= b)continue;
		if (vis[tx][ty] || mpt[tx][ty] == 'X')continue;
		vis[tx][ty] = 1;
		DFS(tx, ty, t - 1);
		vis[tx][ty] = 0;
	}
}
int main()
{
	while (cin >> a>>b>>time){
		if (!a&&!b&&!time)return 0;
		int i,j,si,sj,di=-1,dj=-1;
		ans = 0;
		memset(mpt, 0, sizeof(mpt));
		memset(vis, 0, sizeof(vis));
		for (i = 0; i < a; i++){ 
			cin >> mpt[i]; 
		}
		for (i = 0; i < a; i++)
		{
			for (j = 0; j < b; j++){
				if (mpt[i][j] == 'S'){
					si = i; sj = j;
					vis[si][sj] = 1;
				}
				else if (mpt[i][j] == 'D'){
					di = i; dj = j;
				}
			}
		}

				if (di == -1)ans = 0;//没有门,剪枝
				else if ((si + sj + time) % 2 != (di + dj) % 2)ans = 0;//**奇偶性剪枝
				else DFS(si, sj, time);

				if (ans)cout << "YES" << endl;
				else cout << "NO" << endl;
	}
	return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值