Dungeon Master(bfs)

本文探讨了在3D迷宫中寻找最短路径的方法,包括输入格式、输出格式及解决算法,帮助玩家理解如何使用BFS算法进行路径搜索。

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

E - DungeonMaster
TimeLimit:1000MS     MemoryLimit:65536KB     64bitIO Format:%I64d &%I64u

Description

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

Is an escape possible? If yes, how long will ittake? 

Input

The input consists of a number of dungeons. Eachdungeon 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 thedungeon. 
R and C are the number of rows and columns making up the plan ofeach level. 
Then there will follow L blocks of R lines each containing Ccharacters. Each character describes one cell of the dungeon. Acell full of rock is indicated by a '#' and empty cells arerepresented by a '.'. Your starting position is indicated by 'S'and the exit by the letter 'E'. There's a single blank line aftereach level. Input is terminated by three zeroes for L, R andC.

Output

Each maze generates one line of output. If it ispossible to reach the exit, print a line of theform 
Escaped in x minute(s).

where x is replaced by the shortest time it takes toescape. 
If it is not possible to escape, print theline 
Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

#include
#include
#include
#include
using namespace std;

int dir[2]={1,-1};
char arr[30][30][30];
int l,r,c;
struct point
{
    int row,col,level;
    int step;
}p;
int bfs(point s,point e)
{
    int map[r][c][l];
	memset(map,0,sizeof(map));
    queue que;
    point p;
    int x=0,y=0,z=0;
    while(true)
    {
		
		if(s.col==e.col&&s.level==e.level&&s.row==e.row)
		{
			return s.step;
		}
		for(inti=0;i<6;i++)
		{
			if(i<2)
			{
				x=s.col+dir[i];
				y=s.row;
				z=s.level;
			}
			else if(i>=2&&i<4)
			{
				y=s.row+dir[i%2];
				x=s.col;
				z=s.level;
			}
			else if(i>=4&&i<6)
			{
				z=s.level+dir[i%2];
				x=s.col;
				y=s.row;
			}
			if(x<0||x>r-1||y<0||y>c-1||z<0||z>l-1)
				continue;
			if((!map[x][y][z])&&(arr[x][y][z]=='.'||arr[x][y][z]=='E'))
			{
				p.col=x;
				p.level=z;
				p.row=y;
				map[x][y][z]=1;
				p.step=s.step+1;
				que.push(p);
			}
			
		}
		if(que.empty())
			return 0;
		s=que.front();
		que.pop();
    }
	
}

int main()
{
    point s;
    point e;
	while(cin>>l>>r>>c)
	{
		if(l==0&&r==0&&c==0)
		{
			break;
		}
		for(int k=0;k
		{
			for(inti=0;i
			{
				for(int j=0;j
				{
					cin>>arr[i][j][k];
					if(arr[i][j][k]=='S')
					{
						s.col=i;
						s.row=j;
						s.level=k;
						s.step=0;
					}
					if(arr[i][j][k]=='E')
					{
						e.col=i;
						e.row=j;
						e.level=k;
						e.step=0;
					}
				}
			}
		}
		int minute = bfs(s,e);
		if(minute==0)
			cout<<"Trapped!"<<endl;
		else
		{
			cout<<"Escaped in "<<minute<<"minute(s)."<<endl;
		}
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值