poj_2251

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24311   Accepted: 9425

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!
题目链接:poj2251

/*题目大意:l,r,c。表示l层,r行,c列。从起点s到终点e的最小步数、其中.为通道,#为墙。只能上下左右走,也能从上一层跳到相对应的下一层
  算法分析:三维bfs搜索 
  坑点:每次处理完后要清空队列中所有元素 
*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;

int L, R, C;
int vis[40][40][40];
int l[6] = {1, -1, 0, 0, 0, 0}, r[6] = {0, 0, 1, -1, 0, 0}, c[6] = {0, 0, 0, 0, 1, -1};
char map[40][40][40];
struct node {
	int z, x, y;
	int time;
};

node start, end;
queue <node> q;

int judge(int a, int b, int c) {
	if (a>=0 && a<L && b>=0 && b<R && c>=0 && c<C)	return 1;
	return 0;
}

int dfs() {
	
	while (!q.empty()) {
		node cur, next;
		cur = q.front();
		q.pop();
		if (cur.z == end.z && cur.x == end.x && cur.y == end.y && judge(cur.z,cur.x,cur.y))		return cur.time;
		for (int i = 0; i<6; i++) {
			next.z = cur.z + l[i];
			next.x = cur.x + r[i];
			next.y = cur.y + c[i];
			next.time = cur.time + 1;
			if (judge(next.z, next.x, next.y) && map[next.z][next.x][next.y] != '#' && !vis[next.z][next.x][next.y]) {
				if (next.z == end.z && next.x == end.x && next.y == end.y)	return next.time;
				q.push(next);
				vis[next.z][next.x][next.y] = 1;
			}
		}
	}
	return -1;
}

int main() {
	while (cin >> L >> R >> C && (L+R+C)) {
		memset(map, 0, sizeof(map));
		memset(vis, 0, sizeof(vis));
		
		for (int i = 0; i<L; i++) {
			for (int j = 0; j<R; j++) {
				for (int k = 0; k<C; k++) {
					cin >> map[i][j][k];
					if (map[i][j][k] == 'S') {
						start.z = i;
						start.x = j;
						start.y = k;
						start.time = 0;
						vis[i][j][k] = 1;
						q.push(start);
					}
					else if (map[i][j][k] == 'E') {
						end.z = i;
						end.x = j;
						end.y = k;
					}
				}
			}
		}
		int ans = dfs();
		if (ans == -1)	cout << "Trapped!" << endl;
		else 	cout << "Escaped in " << ans << " minute(s)."<< endl;
		while (!q.empty()) 	q.pop();
	}
	return 0;
}


posted @ 2016-03-24 18:03  Tovi  阅读(119)  评论(0编辑  收藏  举报