Under construction

Still being built. Things may change

The Sangbeom Building

Interview

Time limit1sMemory limit128 MB

Summary
Given a 3D grid of blocked and open cells with a start and an exit, find the minimum number of moves (or report impossible).
Level

Medium4 of 10

Topics
BFS, Graph, Shortest path, Matrix
Solved
No attempts yet

Problem

You are trapped inside the Sangbeom Building. What is the fastest way to escape?

The building is made up of unit cubes, each with side length 11. Every cube is either filled with gold (impassable) or empty (passable). From your current cell you can move to any of the 66 adjacent cells (east, west, south, north, up, down), and each move takes 11 minute. Diagonal moves are not allowed. The outer surface of the building is entirely blocked by gold, so you can only leave through an exit.

Can you escape the Sangbeom Building? If so, how long will it take?

Input

The input consists of several test cases. Each test case begins with three integers LL, RR, and CC. L (1≤L≤30)L\,(1 \le L \le 30) is the number of floors of the building, and R (1≤R≤30)R\,(1 \le R \le 30) and C (1≤C≤30)C\,(1 \le C \le 30) are the number of rows and columns on each floor.

After that, RR rows of CC characters each are given for every one of the LL floors. Each character represents one cell of the building:

  • # : a cell blocked by gold that cannot be passed
  • . : an empty cell that can be passed
  • S : your starting position
  • E : the exit through which you can escape

There is one blank line between consecutive floors. There is always exactly one starting position and exactly one exit. The end of the input is marked by a line in which LL, RR, and CC are all 00; this line is not processed.

Output

For each building, output the answer on its own line. If you can escape, print:

Escaped in x minute(s).

where x is the minimum time in minutes needed to escape the building. If escaping is impossible, print:

Trapped!

Examples1

  1. Example 1

    Input
    3 4 5
    S....
    .###.
    .##..
    ###.#
    
    #####
    #####
    ##.##
    ##...
    
    #####
    #####
    #.###
    ####E
    
    1 3 3
    S##
    #E#
    ###
    
    0 0 0
    
    Expected output
    Escaped in 11 minute(s).
    Trapped!