Runner Pawns
Time limit1sMemory limit128 MB
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 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.

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 to :
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 the horse can move to , , , , , , , or ; from square it can move only to or .
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 to (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 , the number of pawns (), followed by integers () giving the starting square of each pawn, followed by an integer (), the starting square of the horse. The input ends with a line containing , 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.