Problems > Knight's Journey

Knight's Journey

Difficulty: 1500 4000 ms 128 MB

bfs graphs

On an N×N chessboard, print the minimum number of knight moves from square (r1, c1) to (r2, c2).

Input: one line with five integers: N r1 c1 r2 c2.

Output: the minimum number of knight moves (the board is large enough that it is always reachable for N ≥ 4).

Constraints:

  • 4 ≤ N ≤ 500
  • coordinates are within the board

Sample Tests

Input 1
8 1 1 8 8
Output 1
6
Input 2
5 3 3 3 3
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.