POJ-2251 Dungeon Master

2 篇文章 0 订阅
2 篇文章 0 订阅

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.

OutputEach 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!

Sample Input

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

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

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

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

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

题意:有一个多层的迷宫,S为起点、E为终点,若能逃出迷宫,则输出最短路径,若不能则输出Trapped!创建一个三维数组,再利用BFS(队列)求出最短路。

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

int L,m,n,a[35][35][35],dir[6][3]={{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};//北、东、南、西、上、下六个方向
char str[35][35][35];//创建一个三维数组存储样例
struct node{
	int x,y,z,step;//记录一个点的三个方向的坐标及行走的步数
};

int bfs(int sx,int sy,int sz,int ex,int ey,int ez){
	queue<node>p;//定义一个node型结构体的队列
	node fr,nr;
	fr.x=sx;fr.y=sy;fr.z=sz;fr.step=0;//将起点的坐标分别赋给fr,并且初始步数为0
	a[sx][sy][sz]=1;//访问过标记为1
	p.push(fr);//将fr压入队列
	while(!p.empty()){
		fr=p.front();//将队首元素赋给fr
		p.pop();//弹出队首元素
		if((fr.x==ex)&&(fr.y==ey)&&(fr.z==ez)){
			return fr.step;//若匹配则返回最短路径
		}
		
		for(int i=0;i<6;i++){//依次搜索六个方向
			nr.x=fr.x+dir[i][0];
			nr.y=fr.y+dir[i][1];
			nr.z=fr.z+dir[i][2];不是#(即走得通),未越界,未访问
			if((str[nr.x][nr.y][nr.z]!='#')&&(nr.x>=0&&nr.x<L)&&(nr.y>=0&&nr.y<m)&&(nr.z>=0&&nr.z<n)&&(a[nr.x][nr.y][nr.z]==0)){
				nr.step=fr.step+1;//在之前的基础上步数加1
				a[nr.x][nr.y][nr.z]=1;//标记访问
				p.push(nr);//将可行的路径压入队列
			}
		}
	}
	return -1;
}

int main(){
	int sx,sy,sz,ex,ey,ez;
	while(scanf("%d%d%d",&L,&m,&n)!=EOF){
		if(L==0&&m==0&&n==0) 
		break;
		memset(a,0,sizeof(a));//每读入一个新的样例都要进行初始化
		memset(str,'0',sizeof(str));
		
		for(int i=0;i<L;i++){
			for(int j=0;j<m;j++){
				scanf("%s",&str[i][j]);
				for(int k=0;k<n;k++){
					if(str[i][j][k]=='S'){
						sx=i;sy=j;sz=k;//记录起点
					}
					if(str[i][j][k]=='E'){
						ex=i;ey=j;ez=k;//记录终点
					}
				}
			}
			
		}
		
		int t=bfs(sx,sy,sz,ex,ey,ez);
		if(t==-1){
			printf("Trapped!\n");
		}
		else
		printf("Escaped in %d minute(s).\n",t);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值