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

Source

Ulm Local 1997

一个简单的BFS找最短路径,只不过背景是三维的罢了。。

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int mp[35][35][35];
int vis[35][35][35];

//往六个方向走
const int dx[] = {1, 0, 0, -1, 0, 0};
const int dy[] = {0, 1, 0, 0, -1, 0};
const int dz[] = {0, 0, 1, 0, 0, -1};

int L, R, C;

int sx, sy, sz;

int ans;

struct node {
	int x, y, z;
	int c;
	node(int _x, int _y, int _z, int _c) {
		x = _x;
		y = _y;
		z = _z;
		c = _c;
	}
};

bool bfs(int x, int y, int z) {
	memset(vis, 0, sizeof(vis));
	queue<node> que;
	que.push(node(x, y, z, 0));
	vis[x][y][z] = 1;
	while(!que.empty()) {
		node t = que.front();
		que.pop();
		for(int i = 0; i < 6; i ++) {
			int xx = t.x + dx[i];
			int yy = t.y + dy[i];
			int zz = t.z + dz[i];
			if(xx >= 0 && xx < L && yy >= 0 && yy < R && zz >= 0 && zz < C && mp[xx][yy][zz] != 1
				&& !vis[xx][yy][zz]) {	//好吧,<L,<R,<C统统写成<=了,莫名其妙跪啦好久,傻了。。
					if(mp[xx][yy][zz] == 4) {
						ans = t.c + 1;
						return true;
					}
					que.push(node(xx, yy, zz, t.c + 1));
					vis[xx][yy][zz] = 1;
				}
		}
	}
	return false;
}

int main() {
	while(scanf("%d %d %d", &L, &R, &C) != EOF) {
		if(L == 0 && R == 0 && C == 0) break;

		for(int i = 0; i < L; i ++) {
			for(int j = 0; j < R; j ++) {
				char str[55];
				scanf("%s", str);
				for(int k = 0; k < C; k ++) {
					if(str[k] == '.') {
						mp[i][j][k] = 0;
					}
					else if(str[k] == '#') {
						mp[i][j][k] = 1;
					}
					else if(str[k] == 'S') {
						mp[i][j][k] = 3;
						sx = i; sy = j; sz = k;
					}
					else if(str[k] == 'E') {
						mp[i][j][k] = 4;
					}
				}
			}
		}

//		for(int i = 0; i < L; i ++, printf("\n")) {
//			for(int j = 0; j < R; j++, printf("\n")) {
//				for(int k = 0; k < C; k ++) {
//					printf("%d", mp[i][j][k]);
//				}
//			}
//		}

		ans = 0;
		if(bfs(sx, sy, sz)) {
			printf("Escaped in %d minute(s).\n", ans);
		}
		else {
			printf("Trapped!\n");
		}

	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 06:27:25

POJ - 2251 - Dungeon Master (简单BFS)的相关文章

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

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【bfs】

Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 9   Accepted Submission(s) : 7 Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge

poj 2251 Dungeon Master(三维BFS)(中等)

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

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 m

poj 2251 Dungeon Master(bfs)

题目链接  http://poj.org/problem?id=2251 题意:一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped! 简单的三维bfs随便做一下就可以了. #include <iostream> #include <cstring&g

POJ 2251 Dungeon Master (bfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; char mat[50][50][50]; int vis[50][50][50]; int op[6][3]={0,-1,0, 0,1,0, 1,0,0, -1,0,0 ,0,0,1, 0,0,-1 }; int ok; in

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(地牢大师)

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