[kuangbin带你飞]专题一 简单搜索 B - Dungeon Master

题目

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

    Escaped in x minute(s). 


where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line

    Trapped! 

解题思路

这道题大意就是走迷宫,不过迷宫不是在二维平面上了,而是在三维空间内,因此会有多层,相邻层之间是可以走动的。最终目的是求S点到E点需要的最短时间,没走一步算一秒。迷宫中#位置是不能走的,只有.位置可以走。
遇到求最短路径的问题,一般都会用BFS求解,注意这里的最短路径的前提是每条路径权重相等,否则就不能用BFS了。这道题的迷宫是三维的,那么我们开个三维的数组记录迷宫就可以了。这里有个小点需要注意一下,BFS使用队列来实现,那么每次加入队列的点,以后就不能再加入了,因此初始点加入队列之前要对其做标记,防止后面重复加入。

代码

下面给出AC代码

#include<cstdio>
#include<queue>
using namespace std;

char mp[100][100][100];
int dx[6]={1,-1,0,0,0,0};
int dy[6]={0,0,1,-1,0,0};
int dz[6]={0,0,0,0,1,-1};
int L,R,C;
struct node
{
	int x,y,z,step;
};
bool isvalid(int x,int y,int z)
{
	if(x<0||y<0||z<0||x>R-1||y>C-1||z>L-1||mp[z][x][y]=='#') return false; 
	return true;
}
int bfs(int b_x,int b_y,int b_z)
{
	queue<node> que;
	node new_node,add_node;
	add_node.x = b_x;
	add_node.y = b_y;
	add_node.z = b_z;
	add_node.step = 0;
	mp[b_z][b_x][b_y] = '#';//保证进入队列的点都是不能再走的点,赋值为# 
	que.push(add_node);
	int x,y,z;
	while(!que.empty())
	{
		new_node = que.front();
		que.pop();
		for(int index = 0;index<6;index++)
		{
			x = new_node.x + dx[index];
			y = new_node.y + dy[index];
			z = new_node.z + dz[index];
			if(isvalid(x,y,z))
			{
				if(mp[z][x][y] == 'E') return new_node.step+1;
				mp[z][x][y] = '#';
				add_node.x = x;
				add_node.y = y;
				add_node.z = z;
				add_node.step = new_node.step+1;
				que.push(add_node);
				 
			}
		}

	}
	return -1;
}
int main()
{

	int b_x,b_y,b_z;
	int Time;
	while(scanf("%d%d%d",&L,&R,&C)&&L&&R&&C)
	{
		//Time = 0;
		for(int z = 0;z<L;z++)
		{
			for(int i = 0;i<R;i++)
			{
				scanf("%s",mp[z][i]);
				for(int j = 0;j<C;j++)
				{
					if(mp[z][i][j] == 'S' )
					{
						b_x = i;
						b_y = j;
						b_z = z;
					}

				}
			}
		}
		Time = bfs(b_x,b_y,b_z);
		if(Time==-1) printf("Trapped!\n");
		else printf("Escaped in %d minute(s).\n",Time);

	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值