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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
三维的bfs, 总共可以向6个方向走,走一步步数+1, 可以按行读取地图, 把z轴设为最高维度, 基本过程和二维的bfs类似
#include <iostream> #include <stdio.h> #include <cstring> #include <queue> using namespace std; char maze[31][31][31]; bool v[31][31][31]; int l, r, c; int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; struct node { int x, y, z, t; }now, Next; int bx, by, bz; int ex, ey, ez; int bfs() { now.x = bx; now.y = by; now.z = bz; now.t = 0; v[bz][bx][by] = 1; queue<node> q; q.push(now); while (!q.empty()) { now = q.front(); q.pop(); if (now.z == ez && now.x == ex && now.y == ey) return now.t; for (int i = 0; i < 6; i++) { Next.z = now.z + dir[i][0]; Next.x = now.x + dir[i][1]; Next.y = now.y + dir[i][2]; Next.t = now.t + 1; if (Next.x>=0&&Next.x<r&&Next.y>=0&&Next.y<c&&Next.z>=0&&Next.z<l && !v[Next.z][Next.x][Next.y] && maze[Next.z][Next.x][Next.y] != '#') { q.push(Next); v[Next.z][Next.x][Next.y] = 1; } } } return 0; } int main() { //freopen("1.txt", "r", stdin); while (~scanf("%d%d%d", &l, &r, &c)) { if (l+c+r == 0) break; memset(v, 0, sizeof(v)); for (int i = 0; i < l; i++) for (int j = 0; j < r; j++) scanf("%s", maze[i][j]); for (int i = 0; i < l; i++) for (int j = 0; j < r; j++) for (int k = 0; k < c; k++) { if (maze[i][j][k] == 'S') { bz = i; bx = j; by = k; } if (maze[i][j][k] == 'E') { ez = i; ex = j; ey = k; } } int ans = bfs(); if (ans == 0) printf("Trapped! "); else printf("Escaped in %d minute(s). ", ans); } return 0; }