Problem · Matrix

Reveal a Minesweeper Region

Learn this problem
MediumNuro logoNuroINTERNNEW GRADPHONE SCREEN

Problem statement

You are given a rectangular Minesweeper board. A mine is encoded as -1, and every unrevealed empty cell is encoded as 0.

Reveal the region that begins at the empty cell (startRow, startCol). Two cells are adjacent when their row and column differ by at most one, excluding the cell itself.

When an unrevealed empty cell is reached:

  • Count the mines among its at most eight adjacent cells.
  • If the count is positive, replace the cell with that count and do not expand from it.
  • If the count is zero, replace the cell with -2, then continue the reveal from each adjacent cell that is still 0.

Mines remain -1, and empty cells that are never reached remain 0. Return the resulting board.

Function

revealRegion(board: int[][], startRow: int, startCol: int) → int[][]

Examples

Example 1

board = [[0,0,0,0,0],[0,0,0,-1,0],[0,0,0,0,0],[0,0,0,0,0]]startRow = 3startCol = 0return = [[-2,-2,1,0,0],[-2,-2,1,-1,0],[-2,-2,1,1,1],[-2,-2,-2,-2,-2]]

The reveal begins in the mine-free lower-left region. Every zero-clue cell in that region becomes -2, and its numbered boundary is revealed. Expansion stops at those numbered cells, so the two empty cells beyond the mine remain 0.

Example 2

board = [[0,0,0],[0,0,0]]startRow = 0startCol = 1return = [[-2,-2,-2],[-2,-2,-2]]

The board contains no mines. Every empty cell is connected to the reveal origin and has zero adjacent mines, so every cell becomes -2.

Example 3

board = [[0,-1,0],[0,0,0],[0,0,-1]]startRow = 1startCol = 1return = [[0,-1,0],[0,2,0],[0,0,-1]]

The origin has two adjacent mines, so it is revealed as 2. Because its clue is positive, the reveal does not expand to any neighbor.

Constraints

  • 1 <= board.length <= 200.
  • 1 <= board[r].length <= 200, and every row has the same length.
  • Every initial cell is -1 or 0.
  • 0 <= startRow < board.length and 0 <= startCol < board[0].length.
  • board[startRow][startCol] == 0.

More Nuro problems

drafts saved locally
public int[][] revealRegion(int[][] board, int startRow, int startCol) {
  // write your code here
}
board[[0,0,0,0,0],[0,0,0,-1,0],[0,0,0,0,0],[0,0,0,0,0]]
startRow3
startCol0
expected[[-2,-2,1,0,0],[-2,-2,1,-1,0],[-2,-2,1,1,1],[-2,-2,-2,-2,-2]]
checking account