Hypertheseus
Time limit1sMemory limit128 MB
Given a recursive d-dimensional grid with walls and single T, S, M cells, find the shortest walk T to S to M to T, never touching M before the sword is taken.
- Level
Medium7 of 10
- Topics
- BFS, Graph, Shortest path, Implementation
- Solved
- No attempts yet
Problem
An advertising company produces commercials featuring ancient heroes such as Prometheus, Achilles, and Odysseus. To show how hard life was for these heroes, it runs computer simulations of their most famous ordeals. In this problem we simulate the ordeal of Theseus.
Theseus was an Athenian hero who slew the Minotaur, the half-man, half-bull monster that lived in an inescapable Labyrinth. His real challenge was not killing the monster but finding the way out. Here we simulate that task under the following rules.
- The labyrinth is a -dimensional grid of (hyper)cubes. Each cube is either empty (a corridor) or filled with rock (a wall).
- Theseus moves in steps; each step travels between two neighboring empty cubes. Two cubes are neighbors if and only if their coordinates differ by exactly one in a single dimension and are equal in every other dimension.
- Theseus cannot fight the Minotaur bare-handed, so he must first pick up a sword that lies somewhere in the labyrinth. Before he takes the sword, he may not pass through the cube where the Minotaur resides.
Input
The input consists of several labyrinth descriptions. Each description is given as follows.
- The first line contains a single integer (), the number of dimensions.
- The second line contains space-separated integers , the size of the labyrinth along each dimension in unit cubes, with for every . The total number of cubes in any labyrinth does not exceed .
- A labyrinth map follows, described recursively.
- A two-dimensional labyrinth (of size ) is given as lines of characters each. Every character describes one cube and is either
#(rock),.(free space), or one of the uppercase lettersT(Theseus' position),M(Minotaur), andS(sword). Each ofT,M, andSappears exactly once in the whole labyrinth, and all three of those cubes count as empty and may be walked through. - Each two-dimensional map is followed by one empty line.
- For , a -dimensional labyrinth is a sequence of "layers", each layer being a description of a -dimensional labyrinth.
- A two-dimensional labyrinth (of size ) is given as lines of characters each. Every character describes one cube and is either
The last labyrinth description is followed by a line containing a single 0, which terminates the input; no output is produced for it.
Output
For each labyrinth, print the line
Theseus needs S steps.
where is the smallest number of steps needed to start at Theseus' position, reach the cube holding the sword, then reach the cube holding the Minotaur, and finally return to Theseus' original position (where the exit is assumed to be). It is not possible to leave the labyrinth area; every cube outside the labyrinth counts as rock. The exit may lie in an interior cube and need not be on the "border".
If the situation cannot be solved at all, print
No solution. Poor Theseus!