Problem solution · Python

Maximum Number of Moves to Kill All Pawns

Maximum Number of Moves to Kill All Pawns: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Breadth-first search
Source
walkccc LeetCode Solutions
Length
84 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Maximum Number of Moves to Kill All Pawns, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 84 lines of Python from the credited upstream file 3283.py.
  • The implementation visibly relies on sequence storage, hash lookup, work queue, cached states.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum Number of Moves to Kill All Pawns · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def __init__(self):    self.SIZE = 50    self.MAX = 1_000_000    self.DIRS = ((1, 2), (2, 1), (2, -1), (1, -2),                 (-1, -2), (-2, -1), (-2, 1), (-1, 2))   def maxMoves(self, kx: int, ky: int, positions: list[list[int]]) -> int:    n = len(positions)    positions.append([kx, ky])    hashedPositionToIndex = {}    # dist[i][j] := the minimum distance from positions[i] to positions[j]    dist = [[0] * (n + 1) for _ in range(n + 1)]     for i, (x, y) in enumerate(positions):      hashedPositionToIndex[self._hash(x, y)] = i     for sourceIndex in range(n + 1):      self._bfs(positions, sourceIndex, hashedPositionToIndex, dist)     MAX_MASK = 1 << (n + 1)    # dp[i][mask][turn] := the maximum (Alice) or the minimum (Bob) cost to    # kill all pawns, where i is the current pawn, mask is the set of pawns    # that have been killed, and turn is the current player's turn (0 for Alice    # and 1 for Bob)    dp = [[[0, 0]          for _ in range(1 << (n + 1))]          for _ in range(n + 1)]     for i in range(n + 1):      for mask in range(MAX_MASK - 1):        dp[i][mask] = [-self.MAX, self.MAX]     for mask in range(MAX_MASK - 2, -1, -1):      for i in range(n + 1):        for turn in range(2):          for j in range(n):            if mask >> j & 1:              continue            moves = dist[i][j] + dp[j][mask | 1 << j][1 - turn]            dp[i][mask][turn] = (max(dp[i][mask][turn], moves) if turn == 0 else                                 min(dp[i][mask][turn], moves))     # Returns the maximum cost to kill all pawns, i.e., the original positions    # array without the knight (kx, ky).    return dp[n][1 << n][0]   def _hash(self, x: int, y: int) -> int:    return x * self.SIZE + y   def _bfs(      self,      positions: list[list[int]],      sourceIndex: int,      hashedPositionToIndex: dict[int, int],      dist: list[list[int]]  ) -> None:    """    Computes the distance between positions[sourceIndex] and other positions.    """    sx, sy = positions[sourceIndex]    q = collections.deque([(sx, sy)])    seen = {(sx, sy)}    seenPositions = 0     step = 0    while q and seenPositions < len(positions):      for _ in range(len(q)):        i, j = q.popleft()        hashedPosition = self._hash(i, j)        if hashedPosition in hashedPositionToIndex:          dist[sourceIndex][hashedPositionToIndex[hashedPosition]] = step          seenPositions += 1        for dx, dy in self.DIRS:          x = i + dx          y = j + dy          if x < 0 or x >= self.SIZE or y < 0 or y >= self.SIZE:            continue          if (x, y) in seen:            continue          q.append((x, y))          seen.add((x, y))      step += 1 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗