This page is still under construction.

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

Runner Pawns

Time limit1sMemory limit128 MB

Summary
On an 8x8 board with up to 8 pawns advancing one row per round, find the minimum knight moves to capture all pawns, or report impossible.
Level

Hard8 of 10

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

Problem

Runner Pawns is a single-player variant of chess played on an 8×88 \times 8 board. As in chess, every square holds at most one piece at a time. The pieces are several pawns (the Runner Pawns) and a single horse (a knight), which is the only piece the player controls. The goal is to capture every pawn before any of them reaches the last row and is promoted.

Possible movements of the horse

The horse moves in an "L" shape: it always moves two squares in one direction and one square in the perpendicular direction. In the figure above, H marks the horse's current square and each • marks a square it can reach in a single move. Black and white squares are not distinguished.

The squares are numbered from 11 to 6464:

01 02 03 04 05 06 07 08
09 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64

For example, from square 2222 the horse can move to 55, 77, 1212, 1616, 2828, 3232, 3737, or 3939; from square 5757 it can move only to 4242 or 5151.

The pawns move differently from chess: each one advances exactly one square straight down (never diagonally), and all pawns move at the same time. They advance from the top of the board toward the bottom, so the squares 5757 to 6464 (the last row) are the pawns' goal.

A round consists of one move of the horse, followed by a simultaneous one-square advance of every pawn still on the board.

  • The horse captures a pawn by moving onto the square that pawn occupies; the captured pawn is removed and does not advance.
  • When a pawn reaches the last row it is promoted to a king. The horse then has exactly one move to capture that king; if it fails, the king escapes and the player loses. A pawn that already starts on the last row is a king from the very first round.
  • If the horse moves onto a square that a surviving pawn will occupy during that same round's advance, the horse is captured and the player loses.

The player wins the instant every pawn has been captured. Write a program that decides, for a given starting diagram, whether the horse can win, and if so reports the minimum number of horse moves required.

Input

The input contains several instances, one per line. Each line begins with an integer PP, the number of pawns (0≤P≤80 \le P \le 8), followed by PP integers A1,A2,…,APA_1, A_2, \ldots, A_P (1≤Ai≤641 \le A_i \le 64) giving the starting square of each pawn, followed by an integer HH (1≤H≤641 \le H \le 64), the starting square of the horse. The input ends with a line containing P=0P = 0, which is not to be processed.

Output

For each instance, print a single line. If the horse can capture every pawn before any surviving king escapes and without the horse itself being captured, print the minimum number of horse moves required. Otherwise, print impossible.

Examples3

  1. Example 1

    Input
    1 1 11
    1 60 1
    2 33 60 54
    0
    
    Expected output
    1
    impossible
    3
    
  2. Example 2

    Input
    1 57 42
    0
    
    Expected output
    1
    
  3. Example 3

    Input
    2 2 6 28
    0
    
    Expected output
    5