Minesweeper - Problem
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:
- Mine clicked ('M'): Game over! Change it to 'X'
- Empty square with no adjacent mines ('E'): Change to 'B' and recursively reveal all adjacent unrevealed squares
- 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code