Approach
Depth-first search
For Minesweeper, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 38 lines of Python from the credited upstream file 529.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def updateBoard(self, board: list[list[str]],3 click: list[int]) -> list[list[str]]:4 i, j = click5 if board[i][j] == 'M':6 board[i][j] = 'X'7 return board8 9 DIRS = ((-1, -1), (-1, 0), (-1, 1), (0, -1),10 (0, 1), (1, -1), (1, 0), (1, 1))11 12 def getMinesCount(i: int, j: int) -> int:13 minesCount = 014 for dx, dy in DIRS:15 x = i + dx16 y = j + dy17 if x < 0 or x == len(board) or y < 0 or y == len(board[0]):18 continue19 if board[x][y] == 'M':20 minesCount += 121 return minesCount22 23 def dfs(i: int, j: int) -> None:24 if i < 0 or i == len(board) or j < 0 or j == len(board[0]):25 return26 if board[i][j] != 'E':27 return28 29 minesCount = getMinesCount(i, j)30 board[i][j] = 'B' if minesCount == 0 else str(minesCount)31 32 if minesCount == 0:33 for dx, dy in DIRS:34 dfs(i + dx, j + dy)35 36 dfs(i, j)37 return board38