Welcome to the classic Minesweeper game! ๐ŸŽฎ Your task is to simulate the revealing logic of this beloved puzzle game.

You're given an m x n character matrix representing the game board where:

  • 'M' - An unrevealed mine ๐Ÿ’ฃ
  • 'E' - An unrevealed empty square
  • 'B' - A revealed blank square (no adjacent mines)
  • '1'-'8' - Number of adjacent mines around this revealed square
  • 'X' - A revealed mine (game over!)

You'll receive a click position [clickRow, clickCol] on an unrevealed square. Your job is to simulate the click and return the updated board following these rules:

  1. Mine clicked ('M'): Game over! Change it to 'X'
  2. Empty square with no adjacent mines ('E'): Change to 'B' and recursively reveal all adjacent unrevealed squares
  3. Empty square with adjacent mines ('E'): Change to a digit ('1'-'8') showing the count

Return the board after all possible squares have been revealed.

Input & Output

example_1.py โ€” Basic Mine Click
$ Input: board = [['E','E','E','E','E'],['E','E','M','E','E'],['E','E','E','E','E'],['E','E','E','E','E']], click = [3,0]
โ€บ Output: [['B','1','E','1','B'],['B','1','M','1','B'],['B','1','1','1','B'],['B','B','B','B','B']]
๐Ÿ’ก Note: Clicking at (3,0) reveals a large connected area. The square has no adjacent mines, so it becomes 'B' and triggers recursive revelation of all connected empty squares. Numbers appear where there are adjacent mines.
example_2.py โ€” Direct Mine Hit
$ Input: board = [['B','1','E','1','B'],['B','1','M','1','B'],['B','1','1','1','B'],['B','B','B','B','B']], click = [1,2]
โ€บ Output: [['B','1','E','1','B'],['B','1','X','1','B'],['B','1','1','1','B'],['B','B','B','B','B']]
๐Ÿ’ก Note: Clicking directly on a mine ('M') at position (1,2) ends the game. The mine is revealed by changing 'M' to 'X'. No other squares are affected.
example_3.py โ€” Single Number Reveal
$ Input: board = [['E','E','E'],['E','M','E'],['E','E','E']], click = [0,0]
โ€บ Output: [['1','E','E'],['E','M','E'],['E','E','E']]
๐Ÿ’ก Note: Clicking at (0,0) reveals a square that has exactly 1 adjacent mine. Since there are adjacent mines, it becomes '1' and no recursive revelation occurs.

Constraints

  • m == board.length
  • n == board[i].length
  • 1 โ‰ค m, n โ‰ค 50
  • board[i][j] is either 'M', 'E', 'B', or a digit from '1' to '8'
  • click.length == 2
  • 0 โ‰ค clickr < m
  • 0 โ‰ค clickc < n
  • board[clickr][clickc] is either 'M' or 'E'

Visualization

Tap to expand
Minesweeper Revelation CascadeE1EMEEM221BM1EE111EEStep-by-Step Process:1. Click at (2,0) โ†’ Empty square, no adjacent mines2. Mark as 'B' and explore adjacent squares3. Adjacent squares have mines โ†’ Mark with counts4. Stop expansion at numbered boundariesAlgorithm Insight:โ€ข Empty squares with no adjacent mines form connected components that can be revealed together in one operationโ€ข DFS/BFS naturally explores these connected regions until hitting boundaries (numbered squares or board edges)โœ“ Time: O(m ร— n) - visit each square once
Understanding the Visualization
1
Initial Click
Player clicks on an unrevealed empty square
2
Safety Check
Count adjacent mines - if zero, mark as 'B' and continue
3
Ripple Effect
Recursively reveal all adjacent unrevealed squares
4
Natural Boundary
Stop when reaching squares with adjacent mines (numbered squares)
Key Takeaway
๐ŸŽฏ Key Insight: Minesweeper revelation is a connected components problem where DFS/BFS efficiently explores safe regions until natural boundaries are reached.
Asked in
Amazon 15 Microsoft 12 Google 8 Bloomberg 6
89.2K Views
Medium Frequency
~15 min Avg. Time
1.8K 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