Given a chess board of n
rows (top to bottom) and n
columns (left to right).
In each move, a knight moves either:
- 2 column positions and 1 row position
- 2 row positions and 1 column position
In other words, a move is 2 steps along one axis and 1 step along a perpendicular axis.
Given a starting position A and ending position B, calculate the minimum number of moves needed by the knight to move from A to B if it is possible. If it is not possible, return -1. All moves must remain within the chess board.
Function Description
Complete the function minMoves
in the editor below.
minMoves
has the following parameters:
int n
: the width and height of the square boardint startRow
: the row of the starting locationint startCol
: the column of the starting locationint endRow
: the row of the target locationint endCol
: the column of the target location
Returns
int
: a single integer that denotes the number of moves required or -1 if it is
not possible to reach the target location.
Example 1:

Input: n = 10, startRow = 0, startCol = 0, endRow = 0, endCol = 2
Output: 2
Explanation:The chessboard is of size 10 x 10.Start at the position (0, 0). Move 2 steps down and 1 step right to reach the position (2, 1). Move 1 step right and 2 steps up to reach the position (0, 2). The minimum number of moves to move from the position (0, 0) to the position (0, 2) is 2.
Example 2:

Input: n = 9, startRow = 4, startCol = 4, endRow = 4, endCol = 8
Output: 2
Explanation:The chesse board has a size of 9 x 9.Start at the position (startRow, startCol) = (4, 4). Move 1 step up or down, then 2 steps right to reach either the position (3, 6) or (5, 6). Move 2 steps right and 1 step down or up as necessary to reach the position (4, 8). The minimum number of moves to move from the position(4, 4) to the position (4, 8) is 2.
- 4 ≤ n ≤ 150
- 0 ≤ startRow, startCol, endRow, endCol < n
input:
output: