POJ 2251 Dungeon Master (广搜)

Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18773   Accepted: 7285

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

Sample Input

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

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

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

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

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

将搜索在二维扩展到三维,水题,注意三维坐标在三维数组里下标的表示就好了。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

const int maxl=40;

typedef struct pp
{
	int x,y,z,cnt;
}point;

char map[maxl][maxl][maxl];
int vis[maxl][maxl][maxl];
int sx,sy,sz,ex,ey,ez,L,R,C;

int bfs(){
	int i;
	memset(vis,0,sizeof(vis));
	point s;
	s.x=sx;s.y=sy;s.z=sz;
	s.cnt=0;
	map[sz][sy][sx]='#';		//一定注意这里对应的是sz,sy,sx,而不是sx,sy,sz !!!
	queue<point> que;
	que.push(s);
	while(!que.empty()){
		point t=que.front();
		if(t.x==ex && t.y==ey && t.z==ez) return t.cnt;
		que.pop();
		for(i=-1;i<=1;i+=2)
			if(t.x+i>=1 && t.x+i<=C && map[t.z][t.y][t.x+i]!='#'){
				point tt;
				tt.x=t.x+i;tt.y=t.y;tt.z=t.z;tt.cnt=t.cnt+1;
				map[tt.z][tt.y][tt.x]='#';
				que.push(tt);
			}
		for(i=-1;i<=1;i+=2)
			if(t.y+i>=1 && t.y+i<=R && map[t.z][t.y+i][t.x]!='#'){
				point tt;
				tt.x=t.x;tt.y=t.y+i;tt.z=t.z;tt.cnt=t.cnt+1;
				map[t.z][t.y+i][t.x]='#';
				que.push(tt);
			}
		for(i=-1;i<=1;i+=2)
			if(t.z+i>=1 && t.z+i<=L && map[t.z+i][t.y][t.x]!='#'){
				point tt;
				tt.x=t.x;tt.y=t.y;tt.z=t.z+i;tt.cnt=t.cnt+1;
				map[t.z+i][t.y][t.x]='#';
				que.push(tt);
			}
	}
	return 0;
}

int main()
{
	int i,j,k,t,res;
	while(scanf("%d%d%d",&L,&R,&C)){
		if(L==0 && R==0 && C==0) break;
		for(i=1;i<=L;i++){	//z
			for(j=1;j<=R;j++)	//y
				for(k=1;k<=C;k++){	//x
					scanf(" %c",&map[i][j][k]);
					if(map[i][j][k]=='S'){
						sx=k;sy=j;sz=i;
					}
					if(map[i][j][k]=='E'){
						ex=k;ey=j;ez=i;
					}
				}
			getchar();getchar();
		}
		res=bfs();
		if(res==0)
			printf("Trapped!\n");
		else
			printf("Escaped in %d minute(s).\n",res);
	}
	return 0;
}
时间: 2024-07-30 23:55:16

POJ 2251 Dungeon Master (广搜)的相关文章

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 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 wit

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

poj 2251 Dungeon Master(简单三维广搜)

题意: 之前做过的所有题都是   在一个平面   上搜索 . 本题很新,在一个三维空间里 ,首先   l  x  y   分别代表 l 层   每一层有 x 行   y 列 问从 S 开始   走到 E   最小步是多少    显然用广搜,只是多了一个向上向下的搜索. 注意: 所谓广搜  ,是一层一层的搜,  每一层其步数是一样的   ,可以通过建立一个数组存步数 ,也可以 将步数同时存入队列中 另外搜过的要做标记, 在做标记时  尽量要在里面做标记(就是每搜过一个点就  立马 将之标记)不然一

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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

POJ 2251 Dungeon Master(bfs)

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 dow

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 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