Blenjeel Sand Worms and Color Wriggles

Time limit1sMemory limit128 MB

Summary
A snake of n cells starts filling the left column of an n by m colored grid and must reach the right column, moving one end per wriggle, always keeping n distinct-colored cells; find the minimum number of wriggles.
Level

Medium7 of 10

Topics
BFS, Simulation, Hash map, Implementation
Solved
No attempts yet

Problem

Sand worms slither across the sandy surface of the planet Blenjeel. As the planet's only known inhabitants, they defend their homeland by attacking and devouring, from underneath, anyone who sets foot on their world.

A sand worm must be strong, flexible, and able to slither as quietly as possible. Every teenage sand worm is sent to a six-month boot camp of intense training. The hardest exercise is the famous wriggle test: a cadet must slither from one position to a parallel position hundreds of feet away. Only the toughest worms survive.

That test inspired the following puzzle.

You are given an n×mn \times m board (3≤n≤63 \le n \le 6, 5≤m≤505 \le m \le 50). Every cell has a color written as a digit from 11 to 77 (a board need not use all seven colors). A sand worm of length nn is a chain of nn cells in which consecutive cells are orthogonally adjacent (up, down, left, or right). The worm starts by occupying the entire leftmost column and must reach a state where it occupies the entire rightmost column (either vertical orientation is acceptable).

At every moment the worm must occupy nn cells whose colors are all distinct; it may never simultaneously cover two cells of the same color. (The colors of the leftmost column are guaranteed distinct, so the starting position is always valid.)

A single wriggle works as follows: choose either end of the worm and move that end to an orthogonally adjacent cell. Every other segment then shifts into the cell that its neighbor toward the moving end just occupied, and the opposite end's cell is vacated. For example, from the starting column you may pull the bottom end one step to the right:

You may then pull from the other end to carry the worm through further positions:

The destination of the moving end must lie on the board and must not be occupied by any segment that remains after the shift (it may, however, be the very cell that the opposite end is vacating). After the move, all nn occupied cells must still have distinct colors.

Through a series of wriggles it is sometimes possible to bring the worm entirely into the rightmost column:

Print the minimum number of wriggles needed to move the worm from the leftmost column to the rightmost column, or −1-1 if it is impossible.

Input

The input contains one or more boards. Each board is nn lines of exactly mm characters, where every character is a digit from 11 to 77 giving that cell's color. The colors in the leftmost column of every board are guaranteed to be distinct. Consecutive boards are separated by a single blank line. A line containing only end marks the end of the input, and there is a blank line between the last board and that end line. Both nn and mm are determined by the shape of each board (3≤n≤63 \le n \le 6, 5≤m≤505 \le m \le 50).

Output

For each board, in the order given, print on its own line the minimum number of wriggles needed to move the worm from the leftmost column to the rightmost column, or −1-1 if there is no solution.

Examples2

  1. Example 1

    Input
    12324
    31312
    41431
    
    234234
    342112
    421311
    
    234233
    342112
    421331
    
    41344411122134441231
    22313433414323312312
    12231221312124143323
    
    41251355234115
    13515533543252
    34212412323543
    52454355242421
    
    364311121136362
    151446122112155
    434232633624623
    561614315456464
    234426516251346
    
    end
    
    Expected output
    16
    17
    -1
    39
    31
    58
    
  2. Example 2

    Input
    12345
    23451
    34512
    
    end
    
    Expected output
    12