Minesweeper Tap: Flood-Reveal Safe Cells and Show Mine Counts
Trees & GraphsAuto-graded · 17 test casesLast updated
Simulate one Minesweeper tap on a grid, flood-revealing connected zero-count cells with an iterative traversal and labelling border cells with their 8-neighbour mine counts.
Minesweeper Tap
You are given a Minesweeper grid as a 2D list of single-character strings. Each cell holds one of these symbols:
'M': a hidden mine.'E': a hidden safe cell.'B': a revealed safe cell with no mines among its 8 neighbours.'1'to'8': a revealed safe cell, where the digit is the number of mines among its 8 neighbours.'X': a revealed (detonated) mine.
Neighbours are the up to 8 cells that touch a cell horizontally, vertically, or diagonally. Cells outside the grid do not count.
You are also given tap = [r, c], the position of a single tap. Apply the tap using the rules below and return the resulting grid.
Rules
- If the tapped cell is
'M', change it to'X'. Nothing else changes. - If the tapped cell is already revealed (
'B','1'-'8', or'X'), the grid stays unchanged. - If the tapped cell is
'E'and at least one of its neighbours is a mine, replace it with the count of neighbouring mines as a digit character. No other cell changes. - If the tapped cell is
'E'and none of its neighbours is a mine, replace it with'B'. Then apply the same reveal logic (rules 3 and 4) to each of its neighbours that is still'E', and keep doing this for every cell that becomes'B'.
Only hidden safe cells ('E') are ever revealed by the spreading step. Cells showing a digit never spread the reveal to their neighbours.
You may modify the input grid in place or build a new one, but you must return the final grid.
Function
Implement reveal_cells(grid, tap), which returns the updated grid as a list of lists of single-character strings.
Constraints
m == len(grid)andn == len(grid[0])1 <= m, n <= 1000and1 <= m * n <= 100000- Every cell of
gridis one of'M','E','B','1'-'8'. tap = [r, c]with0 <= r < mand0 <= c < n.- The grid can be large and fully open, so deeply recursive solutions may exceed Python's recursion limit.
Examples
Example 1
Input: reveal_cells([['E', 'E', 'E', 'E', 'E'], ['E', 'M', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E']], [3, 0])
Output: [['E', 'E', '1', 'B', 'B'], ['E', 'M', '1', 'B', 'B'], ['1', '1', '1', 'B', 'B'], ['B', 'B', 'B', 'B', 'B']]
Example 2
Input: reveal_cells([['B', '1', 'E', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']], [1, 2])
Output: [['B', '1', 'E', '1', 'B'], ['B', '1', 'X', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']]
Hints
Hint 1
Handle the two easy cases first: tapping a mine turns it into 'X', and tapping any cell that is not 'E' changes nothing.
Hint 2
For each cell you reveal, count the 'M' cells among its 8 neighbours (with bounds checks). Only when the count is 0 should the reveal spread to the neighbouring 'E' cells.
Hint 3
Use an explicit queue or stack instead of recursion, because the region can contain up to 100000 cells. Mark a cell the moment you enqueue it so it is never queued twice.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a flood fill that stops at numbered cells.
- If the tapped cell is a mine, turn it into
'X'and return. If it is anything other than'E', return the grid unchanged. - Otherwise run a breadth-first traversal starting at the tapped cell. For every cell taken from the queue, count the mines among its 8 in-bounds neighbours.
- If the count is positive, write the digit and do not expand.
- If the count is zero, the cell stays
'B', and every neighbour that is still'E'is added to the queue.
- To avoid queueing a cell several times, claim it when it is enqueued by writing the placeholder
'B'into it. Only'E'cells can be enqueued, so each cell enters the queue at most once. If a claimed cell later turns out to have adjacent mines, its placeholder is overwritten with the digit. The placeholder never affects mine counts, because counting looks only for'M'.
The traversal is iterative because an open 100000-cell board would exceed Python's recursion limit with a recursive DFS.
Why it is correct
- Every cell that gets revealed is reached through a chain of zero-count cells from the tap, which is exactly what rule 4 describes.
- Every cell the recursive definition would reveal is reached, because each zero-count cell pushes all of its hidden neighbours.
- Mines are never changed during the spread, since only
'E'cells are enqueued. So each cell's count depends only on the original mine positions. - Each cell is enqueued at most once, so the loop terminates.
Complexity
- Time: O(m * n). Each cell is dequeued at most once and does a constant amount of work, 8 neighbours checked twice.
- Space: O(m * n) in the worst case for the queue, for example an empty board. The grid is updated in place.
Reference solution
from typing import List
from collections import deque
def reveal_cells(grid: List[List[str]], tap: List[int]) -> List[List[str]]:
r, c = tap
if grid[r][c] == 'M':
grid[r][c] = 'X'
return grid
if grid[r][c] != 'E':
return grid
m, n = len(grid), len(grid[0])
dirs = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
# Claim the start cell with a placeholder so it is queued only once.
grid[r][c] = 'B'
queue = deque([(r, c)])
while queue:
x, y = queue.popleft()
mines = 0
for dx, dy in dirs:
nx, ny = x + dx, y + dy
if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] == 'M':
mines += 1
if mines > 0:
grid[x][y] = str(mines)
continue
for dx, dy in dirs:
nx, ny = x + dx, y + dy
if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] == 'E':
grid[nx][ny] = 'B' # claim on enqueue
queue.append((nx, ny))
return grid