Under construction

Still being built. Things may change

Stranded Robot

Time limit4sMemory limit256 MB

Summary
Find the fewest moves for a robot on a 3D grid of ship blocks and vacuum to reach the teleporter, where each move depends on sunlight and gravity direction.
Level

Hard8 of 10

Topics
BFS, Implementation, Graph
Solved
No attempts yet

Problem

A robot is stranded in the wreckage of an interstellar spaceship. Somewhere in the wreckage is a teleporter that can take the robot to safety.

The spaceship is spinning out of control along all axes. A nearby sun shines light on the wreckage. The ship also has an artificial gravity generator. The artificial gravity always pulls the robot away from the sun, regardless of the ship's orientation.

The robot has solar panels and must rely on solar power to move through the wreckage. When parts of the wreckage block the sun, the robot cannot move. The robot always anchors itself after every move, so it never thrashes about or falls into the void of space.

The wreckage and the area around it form a three-dimensional grid aa of size m×n×pm \times n \times p. Each block is either part of the ship or vacuum. The ship blocks may be disconnected.

The robot starts anchored to a piece of the ship. It decides on its own when to move and when to wait for the sun to shine from a convenient direction.

Gravity can pull the robot in one of 6 directions, 2 along each of the 3 axes. A cell is lit by the sun if there is no wreckage in the direction opposite to gravity from that cell. Before each move, the robot can choose the direction in which gravity pulls it. When making a move, both the original and the destination positions must be lit by the sun at the same time.

The following moves are allowed. The light always shines from above in the images. The blue (darker) block is the robot, and the orange (lighter) blocks are possible destinations.

  1. Moving across the floor

If the robot rests on top of a block, it can move to an adjacent position at the same height. The robot cannot move diagonally. The destination must also be lit by the sun.

  1. Jumping off a cliff

The robot can step off a raised position and then drop down. The drop can be any length.

The robot cannot drop into the void of space, or to an unlit position.

  1. Dropping down

If the robot is lit by the sun and finds itself hanging, it can drop down. This can happen when the gravity direction changes.

The robot cannot drop into the void of space.

The goal is to reach the teleporter, if possible, using the fewest moves. For the teleporter to work, the robot must be firmly anchored to the ship. So the robot must be at the teleporter at the end of a valid move. Dropping through the teleporter does not count. The teleporter does not block the sun or the robot's movement.

Input

The first line contains three integers mm, nn, pp (1≤m,n,p≤5001 \le m, n, p \le 500).

The spaceship and the area around it are described in pp blocks. The kk-th block describes the layer at height kk. Each block consists of nn lines, and the ii-th line of the kk-th block has mm symbols. The jj-th symbol is aijka_{ijk}.

  • * is a full block.
  • - is an empty block.
  • R marks the block with the robot. There is exactly one.
  • T marks the block with the teleporter. There is exactly one.

The robot is guaranteed to be connected to a full block.

Output

Print the minimum number of moves needed to reach the teleporter, or -1 if it cannot be reached.

Examples4

  1. Example 1

    Input
    2 5 1
    R-
    *-
    *-
    *T
    **
    
    Expected output
    1
    
  2. Example 2

    Input
    3 2 1
    R-T
    ***
    
    Expected output
    2
    
  3. Example 3

    Input
    3 3 1
    -R-
    -*-
    -T-
    
    Expected output
    -1
    
  4. Example 4

    Input
    5 4 2
    -R---
    -****
    -****
    -****
    -----
    -----
    *T---
    ----*
    
    Expected output
    5