FastPrepKnight Minimum Moves with a Capturable Bishop

Knight Minimum Moves with a Capturable Bishop

IMC Trading logoIMC Trading● MediumINTERNNEW GRADOA
Learn

Problem statement

You are given an n-by-n chessboard with rows and columns numbered from 0 to n - 1. A knight starts at (startRow, startCol), must reach (endRow, endCol), and shares the board with a bishop at (bishopRow, bishopCol).

A knight moves by changing one coordinate by 2 and the other by 1, with either sign. Every landing square must remain inside the board. While the bishop is present, the knight may not land on a square on either diagonal through the bishop, with two exceptions: it may land on the bishop's own square to capture it, and it may land on the target even if that target is threatened.

After the bishop is captured, its diagonal restrictions disappear for the rest of the route. The bishop never moves. The initial square is permitted even if threatened because the restriction applies to subsequent landings. For this exercise, if the knight and bishop initially share a square, treat the bishop as already captured.

Return the minimum number of knight moves needed to reach the target, or -1 if no route exists. If the start is already the target, return 0.

Function

minKnightMoves(n: int, startRow: int, startCol: int, endRow: int, endCol: int, bishopRow: int, bishopCol: int) → int

Examples

Example 1

n = 8startRow = 4startCol = 2endRow = 2endCol = 6bishopRow = 2bishopCol = 3return = 4

One shortest route is (4,2) → (2,3) → (0,2) → (1,4) → (2,6). The first move captures the bishop, so (1,4) becomes legal. A four-move route avoiding capture also exists.

Example 2

n = 6startRow = 0startCol = 0endRow = 0endCol = 2bishopRow = 0bishopCol = 1return = 2

The route (0,0) → (2,1) → (0,2) uses two legal knight moves. The intermediate square is not threatened.

Example 3

n = 6startRow = 5startCol = 1endRow = 0endCol = 5bishopRow = 2bishopCol = 3return = 3

The route (5,1) → (4,3) → (2,4) → (0,5) reaches the target in three moves without landing on a threatened intermediate square.

Example 4

n = 9startRow = 4startCol = 4endRow = 4endCol = 8bishopRow = 0bishopCol = 1return = 2

The route (4,4) → (3,6) → (4,8) uses two legal knight moves.

Example 5

n = 5startRow = 0startCol = 0endRow = 4endCol = 3bishopRow = 2bishopCol = 2return = 3

The initial square is threatened, but it is permitted. The route (0,0) → (1,2) → (2,4) → (4,3) reaches the target in three moves.

Constraints

  • 4 <= n <= 150
  • 0 <= startRow, startCol, endRow, endCol, bishopRow, bishopCol < n
  • The bishop stays on its square until captured; after capture it attacks no squares.
  • A threatened target is a legal final landing, even before the bishop is captured.
  • The initial square may be threatened. For this exercise, an initial knight-bishop overlap means the bishop is already captured.

More IMC Trading problems

See IMC Trading hiring insights
public int minKnightMoves(int n, int startRow, int startCol, int endRow, int endCol, int bishopRow, int bishopCol) {
    // Write your solution here.
}
n8
startRow4
startCol2
endRow2
endCol6
bishopRow2
bishopCol3
expected4
Checking account…