Labyrinth
InterviewTime limit2sMemory limit1024 MB
Given a 3D grid of h levels with blocked cells, find the shortest time from the start on the top level to the goal on the bottom, where horizontal moves and floor-breaking drops each cost 5 seconds.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Shortest path, Matrix
- Solved
- No attempts yet
Problem
The Prince of Persia opens his eyes and finds himself on the top level of Jaffar's underground labyrinth. The labyrinth has levels stacked directly on top of one another. Each level is a rectangular floor divided into cells. Some cells hold columns that support the ceiling, and the Prince cannot enter those cells.
The Prince can move between two cells of the same level if the cells share a side and neither cell contains a column. This move takes seconds.
The floors of Jaffar's labyrinth are extremely thin, and the Prince can easily smash the floor beneath him with a strong kick, as long as the corresponding cell of the level below has no column. When the floor breaks, the Prince falls down one level without moving horizontally. This action also takes seconds. Of course, if the Prince is already on the bottom level, the floor beneath him will not break.
The Princess, who refused to marry the evil Jaffar, waits for the Prince in one of the cells of the bottom level. Help the Prince find the Princess in as little time as possible.
Input
The first line of the input file contains the natural numbers , and , the height and the horizontal dimensions of the labyrinth (). Then the input file contains blocks describing the levels of the labyrinth from top to bottom.
Each block contains lines of characters each: <<.>> (a dot) denotes an empty cell, <<o>> (the lowercase Latin letter <<o>>) denotes a cell with a column, <<1>> denotes the empty cell where the Prince starts his journey, and <<2>> denotes the empty cell where the Princess is held.
The characters <<1>> and <<2>> each occur in the input file exactly once: the character <<1>> is in the description of the top level, and the character <<2>> is in the description of the bottom level.
Adjacent blocks are separated by one empty line.
Output
Print the minimum time in seconds the Prince needs to find the Princess. Since Good always triumphs over Evil, it is guaranteed that the Prince can do this.