Hopscotch 500
시간 제한2초메모리 제한1024 MB
1부터 k까지 번호가 붙은 n x n 격자에서 1에서 시작해 k까지 순서대로 이동하며, 두 좌표 차이 제곱의 최솟값으로 정의된 거리의 합을 최소화한다.
문제
Do you remember the new art installation from NAC 2020? Well, that artist is at it again, on a grander scale this time, and the new artwork still inspires you---to play a childish game. The art installation consists of a floor with a square matrix of tiles. Each tile holds a single number from to .
You want to play hopscotch on it! You want to start on some tile numbered , then hop to a tile numbered , then , and so on, until you reach a tile numbered .
Instead of the usual Euclidean distance, define the distance between the tile at and the tile at as: \[\min\left[(x_1-x_2)^2, (y_1-y_2)^2\right]\] You want to hop the shortest total distance overall, using this new distance metric. Note that a path with no hops is still a path, and has length . What is the length of the shortest path?
입력
The first line of input contains two space-separated integers () and (), where the art installation consists of an matrix with tiles having numbers from to .
Each of the next lines contains space-separated integers (). These are the numbers in the art installation.
출력
Output a single integer, which is the total length of the shortest path from any tile to any tile using our distance metric, or if no such path exists.