欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ-2251 Dungeon Master(三维空间BFS)

程序员文章站 2022-05-20 20:26:17
...

题目来源:

POJ-2251 Dungeon Master

Description

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 LL, RR and CC (all limited to 30 in size).
LL is the number of levels making up the dungeon.
RR and CC are the number of rows and columns making up the plan of each level.
Then there will follow LL blocks of RR lines each containing CC 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 LL, RR and CC.

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!

Sample Input

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

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

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

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

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

题意:

有一个三维空间的迷宫,从起点出发,找到逃出去的路,如果能逃出去,则输出最短路径的时间,不能则输出Trapped!

view code:

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<algorithm>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<ctime>
#define INF 0x3f3f3f3f
#define maxn  10005
using namespace std;
typedef long long LL;

char map[33][33][33];
typedef struct node{
	int x, y, z;
	int step;
}node;

int l,w,h;
int vis[6][3] = {0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};// 方向
bool dis[33][33][33]; //标记走过的点

int bfs(node n){
	memset(dis, 0, sizeof(dis));
	queue<node> q;
	n.step = 0;
	node d;
	q.push(n);
	while(!q.empty()){
		n = q.front();
		q.pop();
		for(int i=0; i<6; i++){
			int x = n.x+vis[i][0];
			int y = n.y+vis[i][1];
			int z = n.z+vis[i][2];
			if(x>=h || x<0 || y>=w || y<0 || z>=l || z<0 || map[x][y][z] == '#' || dis[x][y][z])
				continue;
			if(map[x][y][z] == 'E') return n.step+1;
			dis[x][y][z] = 1;
			d = {x,y,z,n.step+1};
			q.push(d);
		}
	}
	return -1;
}

int main(){
	while(scanf("%d%d%d",&h,&w,&l)&&(l||w||h)){
		bool flag = false;
		node start;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				scanf("%s",map[i][j]);
				if(!flag){
					for(int k=0; k<l; k++){
						if(map[i][j][k] == 'S'){//找到起点
							flag = true;
							start.x = i;
							start.y = j;
							start.z = k;
						}
					}
				}
			}
		}
		int time = bfs(start);
		if(time == -1){
			printf("Trapped!\n");
		}
		else printf("Escaped in %d minute(s).\n",time);
	}
	return 0;
} 

相关标签: bfs