295. Knight's Shortest Path
MediumBFS
On an n×n board, print the minimum number of knight moves from (r1, c1) to (r2, c2) (1-based), or -1 if unreachable.
Input format
Line 1: n Line 2: r1 c1 r2 c2
Output format
The minimum moves, or -1.
Example 1
Input
8 1 1 8 8
Output
6
Example 2
Input
3 1 1 2 2
Output
-1
Constraints
1 ≤ n ≤ 500
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…