KatInterview

Minesweeper Tap: Flood-Reveal Safe Cells and Show Mine Counts

Trees & GraphsAuto-graded · 17 test cases
MediumAuto-gradedTrees & Graphsbfsflood-fillgrid

Last 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

  1. If the tapped cell is 'M', change it to 'X'. Nothing else changes.
  2. If the tapped cell is already revealed ('B', '1'-'8', or 'X'), the grid stays unchanged.
  3. 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.
  4. 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) and n == len(grid[0])
  • 1 <= m, n <= 1000 and 1 <= m * n <= 100000
  • Every cell of grid is one of 'M', 'E', 'B', '1'-'8'.
  • tap = [r, c] with 0 <= r < m and 0 <= 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.