A courier drone moves across a rectangular board of R rows and C columns like a chess knight: from a cell it may jump to any cell that is two rows and one column away, or one row and two columns away, provided the landing cell is on the board. Cells are addressed by 0-indexed (row, column).
Given a start cell and a target cell, report the fewest jumps needed to move the courier from the start to the target. If the target cannot be reached, report -1. If the start and target are the same cell, the answer is 0.
Line 1: two integers R and C.
Line 2: four integers r1 c1 r2 c2, the start cell (r1,c1) and the target cell (r2,c2).
A single integer: the fewest knight jumps, or -1 if the target is unreachable.
Example 1
Input
8 8 0 0 1 2
Expected
1
Explanation
A single knight jump goes from (0,0) to (1,2), so the answer is 1.
Example 2
Input
3 3 0 0 1 1
Expected
-1
Explanation
On a 3x3 board the center cell (1,1) has no valid knight move into it, so it can never be reached: -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →