KatInterview

Uncover Cells on a Minefield Grid with Flood Expansion

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsbfsgridmatrix

Last 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:

  1. If the grid is empty, or the tap lies outside the grid, make no change.
  2. If the tapped cell is "M", change it to "X". Nothing else changes.
  3. If the tapped cell is anything other than "E" (and not "M"), make no change.
  4. 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.

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 R rows and C columns; 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.