Knight Story
Time limit1sMemory limit128 MB
Assign N knights to N distinct target cells on an infinite chessboard to minimize the total number of knight moves.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Shortest path, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
There is an infinitely large chessboard. knights are placed on distinct cells of this board. In addition, cells of the board are specially marked; these are called target cells. Every target cell differs from the starting cell of every knight.
Write a program that computes the minimum total number of moves needed to move all knights onto the target cells. The knights are identical and indistinguishable, so you may freely decide which knight goes to which target cell. Several knights may occupy the same cell at the same time, but in the end each target cell must contain exactly one knight.
A knight moves in an L-shape: two cells in one direction and one cell perpendicular to it. That is, from a cell it can move to one of the eight cells or .
Input
The input consists of several test cases. The first line of each test case contains the number of knights (which equals the number of target cells) . () Each of the next lines contains two integers and , the starting position of a knight. Each of the following lines contains the coordinates and of a target cell. All coordinates fit in a signed 32-bit integer.
The last line of the input contains a single , which marks the end of the input.
Output
For each test case, print one line in the following format.
k. m
Here is the test case number (starting from 1) and is the minimum total number of moves needed to move all knights onto the target cells.