Horseback Riding
Time limit1sMemory limit512 MB
Move k knights on an 8x8 board into the lowest rows, leftmost cells first, in at most 1500 knight moves without two knights ever sharing a cell.
- Level
Medium7 of 10
- Topics
- Simulation, BFS, Greedy
- Solved
- No attempts yet
Statement
It is well known that chess is a sport. Dima, who is into horseback riding, considers it boring and not a sport at all. His friend Sasha decided to show him how complex and interesting chess can be.
Sasha placed knights on an chessboard. The knights are on a promenade and want to return to their stables, but they do not remember the way home.
The knights are in their stables if the following conditions hold. The lowest rows are filled with knights, and the next row contains knights only in its leftmost cells. If , then knights take the leftmost positions in that row.

The target cells for knights, one per knight.
Knights move as they do in chess: exactly two cells in one direction, then exactly one cell in another direction.

Possible moves of a knight
Knights move one at a time, in any order. At any moment, at most one knight can occupy a cell. Dima spent two days on this problem. Find a sequence of knight moves such that no two knights ever occupy the same cell and all knights end up in their stables. You do not have to minimize the number of moves, but the total must not exceed 1500.
Input
The first line contains an integer , the number of knights on the board (). Each of the following lines contains one knight position in the form xy, where x is a column letter and y is a row digit. Columns run from a (left) to h (right), and rows run from 1 (bottom) to 8 (top).
Output
On the first line, print the number of moves. Then print the moves one per line, in the order they are made, in the form xy-zt, where x and z are column letters and y and t are row digits. You do not have to minimize the number of moves, but the total must not exceed 1500.