#t449. 三维迷宫

三维迷宫

3D Maze

Problem Description

You are trapped inside a three-dimensional space and must find the shortest path to escape!

  • The space is built out of unit cubes.
  • Each step to an adjacent cell (up / down / north / south / east / west) takes one minute.
  • You may not move diagonally, and all sides are sealed.

Is it possible to escape? If so, how many minutes does it take?

Input Format

There are multiple test cases. One test case describes a single 3D maze:

  • The first line of the whole input gives the number of such spaces (mazes).
  • The description of each space starts with one line containing LL, RR and CC (each ≤30\le 30).
  • LL is the height (number of layers) of the space.
  • RR and CC are the number of rows and columns of each layer.
  • Then follow LL layers of the dungeon, each with RR rows of CC characters.
  • Each character is one cell: # is a wall (impassable), . is an empty cell, S is your starting cell, and E is the exit.
  • A blank line separates consecutive layers.

The input ends when LL, RR and CC are all 00.

Output Format

For each space, print one line.

  • If you can escape, print:
Escaped in x minute(s).

where xx is the minimum number of minutes needed to get out.

  • If escape is impossible, print:
Trapped!

These two messages must be printed exactly as shown (one "minute" or more — the s is always present) — the judge compares them character by character.

Sample Input

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

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

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

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

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Constraints

1≤L,R,C≤301 \le L, R, C \le 30.

Time limit 11 s, memory 10241024 KiB per test case.

Hint

  • This is a 3D BFS. Each cell has up to 6 neighbours instead of 4; the rest of the idea is identical to a plain 2D shortest path.
  • Skip the first space (read L,R,CL, R, C); if all three are 00 you are done.
  • Watch the blank line between layers — it is part of the input and your parser must consume it.