Uncover Cells on a Minefield Grid with Flood Expansion
Trees & GraphsAuto-graded · 18 test casesLast updated
Apply a Minesweeper-style tap to a grid, expanding through zero-mine regions with breadth-first search and labeling cells with adjacent mine counts.
You are given a rectangular minefield as a grid of single-character strings, and one tap position. Apply the tap and return the resulting grid.
Each cell holds one of these symbols:
"M": a hidden mine."E": a hidden empty cell (no mine)."B": an already uncovered empty cell with no mines among its 8 surrounding cells."1"to"8": an already uncovered empty cell, where the digit is the number of mines among its 8 surrounding cells."X": an uncovered mine.
The tap is given as [row, col]. Apply these rules:
- If the grid is empty, or the tap lies outside the grid, make no change.
- If the tapped cell is
"M", change it to"X". Nothing else changes. - If the tapped cell is anything other than
"E"(and not"M"), make no change. - If the tapped cell is
"E", uncover it:- Count the mines (
"M") among its up-to-8 neighbors (horizontal, vertical and diagonal neighbors that are inside the grid). - If the count is greater than zero, replace the cell with that count as a digit string (for example
"3"). Do not uncover any neighbors from this cell. - If the count is zero, replace the cell with
"B"and uncover each of its neighbors that is still"E"by applying this same step to them, repeating until no more cells can be uncovered.
- Count the mines (
Only cells that are "E" are ever uncovered by expansion. Mines are never changed by expansion, and cells that were already uncovered are left as they are.
Return the grid after applying the tap. Modifying the input grid in place and returning it is acceptable.
Constraints
- The grid has
Rrows andCcolumns; every row has the same length. - Cells contain only
"M","E","B","X", or"1"through"8". - The tap is a list of two integers
[row, col]. - The tap may be out of bounds, and the grid may be empty; handle both without error.
- An iterative approach is recommended, since expansion may cover the whole grid.
Examples
Example 1
Input: uncover_cells([['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'M', 'E', 'E'], ['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E']], [3, 0])
Output: [['B', '1', 'E', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']]
Example 2
Input: uncover_cells([['B', '1', 'E', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B']], [0, 2])
Output: [['B', '1', '1', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B']]
Example 3
Input: uncover_cells([['E', 'M', 'E'], ['E', 'E', 'E'], ['E', 'E', 'E']], [0, 1])
Output: [['E', 'X', 'E'], ['E', 'E', 'E'], ['E', 'E', 'E']]
Hints
Hint 1
Handle the trivial cases first: empty grid, out-of-bounds tap, tapped mine, and tapped cell that is not hidden-empty.
Hint 2
Use a queue starting from the tapped cell. When you take a cell out, count mines among its in-bounds 8 neighbors.
Hint 3
Only a zero-count cell spreads to neighbors, and only to neighbors still marked E. Skip any dequeued cell that is no longer E so nothing is processed twice.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
First dispose of the cases that need no search: an empty grid, an out-of-bounds tap, a tapped mine (turn it into X), and a tapped cell that is not a hidden empty E (leave everything alone).
Otherwise run a breadth-first search from the tapped cell. For each cell taken off the queue:
- If it is no longer
E, skip it. It may have been queued more than once and already handled. - Look at its in-bounds neighbors among the 8 directions and count the
Mcells. - If the count is positive, write the digit into the cell and stop there, so the search does not continue through numbered cells.
- If the count is zero, write
Band enqueue every neighbor that is stillE.
Why it is correct
Uncovering depends only on a cell's own neighbor count, so the final result does not depend on processing order. A zero-count cell always opens all its hidden neighbors, and a numbered cell never opens any. Therefore BFS reaches exactly the cells that the rules say must open. Writing the new symbol when a cell is processed, together with the E check, guarantees every cell is finalized once. Mines are never queued, since only E neighbors are enqueued.
Complexity
Each cell is finalized at most once and does constant work (8 neighbors). It may be enqueued a bounded number of times (at most 8). Time is O(R * C) and extra space is O(R * C) for the queue.
Reference solution
from collections import deque
from typing import List
def uncover_cells(grid: List[List[str]], tap: List[int]) -> List[List[str]]:
if not grid or not grid[0]:
return grid
rows, cols = len(grid), len(grid[0])
r, c = tap
if not (0 <= r < rows and 0 <= c < cols):
return grid
if grid[r][c] == 'M':
grid[r][c] = 'X'
return grid
if grid[r][c] != 'E':
return grid
directions = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1),
]
queue = deque([(r, c)])
while queue:
cur_r, cur_c = queue.popleft()
if grid[cur_r][cur_c] != 'E':
continue
mines = 0
neighbors = []
for dr, dc in directions:
nr, nc = cur_r + dr, cur_c + dc
if 0 <= nr < rows and 0 <= nc < cols:
neighbors.append((nr, nc))
if grid[nr][nc] == 'M':
mines += 1
if mines:
grid[cur_r][cur_c] = str(mines)
else:
grid[cur_r][cur_c] = 'B'
for nr, nc in neighbors:
if grid[nr][nc] == 'E':
queue.append((nr, nc))
return grid