Maximum Number of Moves to Kill All Pawns - Problem

There is a 50 x 50 chessboard with one knight and some pawns on it. You are given two integers kx and ky where (kx, ky) denotes the position of the knight, and a 2D array positions where positions[i] = [xi, yi] denotes the position of the pawns on the chessboard.

Alice and Bob play a turn-based game, where Alice goes first. In each player's turn:

  • The player selects a pawn that still exists on the board and captures it with the knight in the fewest possible moves.
  • Note that the player can select any pawn, it might not be one that can be captured in the least number of moves.
  • In the process of capturing the selected pawn, the knight may pass other pawns without capturing them. Only the selected pawn can be captured in this turn.

Alice is trying to maximize the sum of the number of moves made by both players until there are no more pawns on the board, whereas Bob tries to minimize them.

Return the maximum total number of moves made during the game that Alice can achieve, assuming both players play optimally.

Note that in one move, a chess knight has eight possible positions it can move to, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.

Input & Output

Example 1 — Basic Game
$ Input: kx = 1, ky = 1, positions = [[0,0]]
Output: 4
💡 Note: Knight at (1,1) needs 4 moves to reach pawn at (0,0). Since there's only one pawn, Alice captures it in 4 moves, total = 4.
Example 2 — Two Pawns
$ Input: kx = 0, ky = 2, positions = [[1,1],[2,2],[3,3]]
Output: 8
💡 Note: Alice tries to maximize moves by choosing the pawn that leads to maximum total, while Bob minimizes on his turn.
Example 3 — Strategic Choice
$ Input: kx = 0, ky = 0, positions = [[1,2],[2,4]]
Output: 10
💡 Note: Alice chooses optimally to maximize total moves across all turns, considering Bob will minimize.

Constraints

  • 1 ≤ positions.length ≤ 15
  • positions[i].length == 2
  • 0 ≤ kx, ky, positions[i][0], positions[i][1] ≤ 49
  • All positions are unique
  • positions[i] ≠ [kx, ky]

Visualization

Tap to expand
INPUTKnight (1,1)Pawns: (0,0), (2,2), (4,4)Alice: Maximize total movesBob: Minimize total movesKnight moves in L-shape:2 squares + 1 perpendicularALGORITHM1Precompute distances (BFS)2Use bitmask for game state3Apply minimax with memoization4Alice MAX, Bob MIN recursionGame Tree Node:(pos=1, mask=110, Alice)→ cached resultRESULT15Maximum Total MovesAlice achieves optimallyGame sequence:Alice: 4 moves to (0,0)Bob: 6 moves to (2,2)Alice: 5 moves to (4,4)Total: 4 + 6 + 5 = 15Key Insight:This is a classic minimax game where Alice maximizes total moves while Bob minimizes.The optimal strategy uses memoization with bitmask to represent remaining pawns efficiently.TutorialsPoint - Maximum Number of Moves to Kill All Pawns | Minimax with Memoization
Asked in
Google 15 Meta 12 Amazon 8
8.5K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen