HDU1010 Tempter of the Bone(小狗是否能逃生----DFS)

Tempter of the Bone

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。
搜索时要用到的剪枝:
1.如果当前时间即步数(step) >= T 而且还没有找到D点,则剪掉。
2.设当前位置(x, y)到D点(dx, dy)的最短距离为s,到达当前位置(x, y)已经花费时间(步数)step,那么,如果题目要求的时间T - step < s,则剪掉。
3. 对于当前位置(x, y),如果,(T-step-s)是奇数,则剪掉(奇偶剪枝)。
4.如果地图中,可走的点的数目(xnum) < 要求的时间T,则剪掉(路径剪枝)。

在一个n行m列的迷宫中,每一步只能向上、下、左、右中任意方向走一格,迷宫中有围墙的地方是无法到达的。从起点s开始,能否刚好走t步,到达e。

迷宫回溯剪枝(https://blog.csdn.net/code_pang/article/details/8839432)
解题思路:
深搜,只不过要加上奇偶剪枝,要不然该TLE了。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int N, M, T, sx, sy, ex, ey;
char map[6][6];
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool flag;
int v[6][6];
int dia(int x,int y)
{
	return abs(x -ex)+abs(y -ey); // 当前点(x,y)到终点(ex,ey)的最短距离
}
void dfs(int x,int y,int step)
{
	if (flag) return;
	if (map[x][y]== 'D'&&step==T) {
		flag=true;
		return;
	}
	if (step>=T) return;                    // 当前时间即步数(step) >= T 而且还没有找到D点
	int dis=T-step-dia(x,y);
	if (dis<0||dis%2) return;           // 剩余步数小于最短距离或者满足奇偶剪枝条件
	for ( int i=0;i<4;i++)
        {
		int tx=x+dir[i][0];
		int ty=y+dir[i][1];
		int tstep=step + 1;
		if ( tx >= 0 && tx < N && ty >= 0 && ty < M && map[tx][ty] != 'X' && !v[tx][ty]) {
			v[tx][ty]=1;
			dfs(tx,ty,tstep);
			v[tx][ty]=0;
		}
	}
}
int main()
{
	while (~scanf("%d%d%d",&N,&M,&T)&&(N+M+T))
        {
        memset(v,0,sizeof(v));
		flag=false;
		int xnum = 0;// 记录'X'的数量
        getchar();
		for(int i = 0; i < N; i += 1 )
            {
			for ( int j = 0; j < M; j += 1 )
			 {
				scanf("%c",&map[i][j]);
				if ( map[i][j] == 'S' )
				{
					sx = i;
					sy = j;
					v[i][j] = 1;
				}
				else if ( map[i][j] == 'D' )
				{
					ex = i;
					ey = j;
				}
				else if ( map[i][j] == 'X' )
				 {
					xnum++;
				}
			}
			getchar();
		}

		if (N*M-xnum>T)
		{ // 可通行的点必须大于要求的步数,路径剪枝。
			dfs(sx,sy,0);
		}
		if (flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值