Chessboard Tour 1
Time limit2sMemory limit512 MB
On an N by N board holding distinct numbers, find the fewest seconds (each a single piece move or a piece swap) to travel through squares 1, 2, ..., N^2 in order using a knight, bishop, and rook.
- Level
Hard8 of 10
- Topics
- BFS, Shortest path, Graph, Dynamic programming
- Solved
- No attempts yet
Problem
There is an chessboard, and each square contains a distinct integer from 1 to . Jihak wants to play an interesting game using this board.
The pieces Jihak has are a knight, a bishop, and a rook. First, he places one piece on the square containing 1. Then he wants to move it through 1, 2, ..., in that order.
He first places one of the knight, bishop, and rook on 1. Then he moves the piece to the square containing 2. When moving from 1 to 2, the piece may visit squares containing other numbers. Next he moves it to the square containing 3, ..., and to the square containing . Visiting the same square multiple times is also allowed.
In one second, Jihak can either move the piece on the board or change it to a different piece.
Find the minimum time needed to start at 1, visit 2, 3, ..., , and arrive at .
Input
The first line gives the size of the chessboard, ().
From the second line, lines give the numbers written on the chessboard.
Output
On the first line, print the minimum time needed for the visit described in the problem.