Road Rally

Time limit1sMemory limit128 MB

Summary
Find the shortest time for a momentum-based motorcycle to visit checkpoints 0 through the last one in order on a grid with walls.
Level

Medium7 of 10

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

Problem

Consider a race track laid out on a rectangular grid.

A lowercase x or an uppercase X marks a wall or barrier. Each numeric digit marks a checkpoint. A motorcycle starts on checkpoint 0 and must visit every numbered checkpoint in increasing order, finishing the course on the highest-numbered checkpoint.

The motorcycle moves under momentum, as follows. During the first second it must move to one of the 8 cells neighbouring its starting cell. During every second after that, let PP be the cell reached by repeating the exact horizontal and vertical displacement of the previous move; the motorcycle may then move to PP or to any of the 8 cells neighbouring PP. In other words, each component of the velocity may change by at most 1 per second. The motorcycle may never land outside the grid or on an x/X, but it may leap over one or more walls as long as it lands on an empty cell or a checkpoint.

For example, on the layout above the rider might follow the sequence of cells labelled a, b, c, …

reaching the first checkpoint in 6 seconds.

Write a program that finds the shortest time needed to start on 0 and finish on the last checkpoint, landing on each intermediate checkpoint in the order given by the digits. The motorcycle may pass over a checkpoint out of order on its way elsewhere, but it is only credited with visiting that checkpoint once it has already visited every lower-numbered checkpoint.

Input

The input contains several race courses. Each course begins with a line containing two integers ww and hh, the width and the height of the track, with 1≤w≤401 \le w \le 40 and 1≤h≤401 \le h \le 40. A line containing 0 0 marks the end of the input.

The header line is followed by hh lines of exactly ww characters each, describing the track as explained above. Every track has an area of at least 2 and contains at least two checkpoints (0 and 1) and at most 10 checkpoints. When more than two checkpoints are present, they are numbered in a strict, gap-free sequence.

Output

For each course, print on its own line a single integer: the minimum number of seconds needed to complete the course. If the course cannot be completed, print -1 instead.

Examples3

  1. Example 1

    Input
    5 5
    xxxxx
    x 0 x
    x 12x
    x   x
    xxxxx
    10 3
    xxxxxxxxxx
    x 0x  1  x
    xxxxxxxxxx
    10 2
    xxxxxxxxxx
    x 0xx 1  x
    30 15
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxx      xxxxx      xxx  xx
    xxxxx        xxx       xx    x
    xx     xx     x        x     x
    x      xx        2     x  x  x
    x      xx                 x  x
    x      xxxxxxxxxxxxxx     x  x
    x      xxxxxxxxxxxxxxxxxxxx  x
    x  3     xxxxxxxxxxxxxx      x
    x          xxxxxxxx          x
    x                            x
    x  xxx                xxx 1  x
    x  xxx       0         x     x
    x                            x
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    0 0
    
    Expected output
    2
    5
    -1
    16
    
  2. Example 2

    Input
    2 1
    01
    0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    7 3
    xxxxxxx
    x01  2x
    xxxxxxx
    0 0
    
    Expected output
    3