This page is still under construction.

Parts of this page are still being built. What you see may change.

Robots

Time limit2sMemory limit128 MB

Summary
Find the minimum number of pushes to merge n robots (n <= 9) into one on a grid, where robots slide until blocked and plates turn them 90 degrees.
Level

Hard8 of 10

Topics
BFS, Graph, Simulation, Bit manipulation
Solved
No attempts yet

Problem

The engineers at VRI (Voltron Robotics Institute) have built a swarm of nn robots. Any two compatible robots that stand on the same grid cell can merge to form a composite robot.

The robots are labeled from 11 to nn (n≤9n \le 9). Two robots are compatible if their labels form a consecutive range. Initially, each of the nn robots carries one unique label. A composite robot formed by merging two or more robots is assigned two labels: the minimum and the maximum labels among the robots that merged into it.

For example, robot 22 can only merge with robot 33 or robot 11. If robot 22 merges with robot 33, a composite robot 2-3 is formed. If robot 2-3 merges with robot 4-6, a composite robot 2-6 is formed. The robot 1-nn is formed once all robots have merged.

The engineers place the nn robots in a room made of w×hw \times h grid cells, surrounded by walls. Some cells are occluded and cannot be entered by the robots. Each cell can hold one or more robots, and a robot always occupies exactly one cell. Initially, every robot is placed on a distinct cell.

The robots are primitive. After an engineer pushes a robot, it can only move in a straight line along the x-axis or the y-axis. Once pushed in one of the four axis-parallel directions, the robot keeps moving in that direction until it is blocked by an occlusion or a wall. After the robot stops, it scans for compatible robots on the same cell and merges with any it finds into a larger robot. Merging repeats until no further merge is possible.

To help the robots change direction, the engineers place rotating plates on some cells. A plate rotates either clockwise or anti-clockwise. A robot that moves onto a cell with a rotating plate always turns its moving direction by 90 degrees in the same rotational direction as the plate. If a robot is pushed while resting on a rotating plate, it turns 90 degrees first and then moves off in a straight line, in a direction perpendicular to the direction it was pushed in.

Only one robot can move at a time.

Your task is to find the minimum number of pushes needed so that all nn robots are merged into one, if that is possible.

Input

The first line contains three integers nn, ww, and hh, separated by spaces.

Each of the next hh lines contains ww characters describing one row of the room. Each character represents one cell:

  • A digit ('1' to '9') means a robot with that label starts on the cell.
  • 'x' means the cell is occluded.
  • 'A' means the cell holds a plate rotating anti-clockwise.
  • 'C' means the cell holds a plate rotating clockwise.
  • '.' marks every other (empty) cell.

Constraints: n≤9n \le 9, w≤500w \le 500, h≤500h \le 500.

Output

Print a single line: the minimum number of pushes needed to merge all nn robots into one, or -1 if merging is impossible.

Hint

In the room from the first test case, the following 5 pushes merge all robots optimally:

  1. Push robot 3 rightward. It moves right, reaches a rotating plate, turns anti-clockwise, and continues upward, finally stopping in front of the wall.
  2. Push robot 4 upward. It moves up, stops in front of the wall, and merges with robot 3 to form robot 3-4.
  3. Push robot 2 upward. It moves up, reaches a rotating plate, turns anti-clockwise, hits a wall, and stops.
  4. Push robot 2 rightward. It turns anti-clockwise, moves up, stops at the corner, and merges with robot 1 to form robot 1-2.
  5. Push robot 3-4 leftward. It moves left, stops at the corner, and merges with robot 1-2.

Examples1

  1. Example 1

    Input
    4 10 5
    1.........
    AA...x4...
    ..A..x....
    2....x....
    ..C.3.A...
    
    Expected output
    5