Minesweeper

Let's play the minesweeper game! You are given an `m x n` char matrix `board` representing the game board where: - `'M'` is an unrevealed mine, - `'E'` is an unrevealed empty square, - `'B'` is a revealed blank square with no adjacent mines (above, below, left, right, and all 4 diagonals), - a digit `'1'` to `'8'` is a revealed square showing how many mines are adjacent to it, - `'X'` is a revealed mine. You are also given `click = [clickr, clickc]`, the position of the next click among the unrevealed squares (`'M'` or `'E'`). Return the board after revealing this position according to these rules: 1. If a mine `'M'` is revealed, the game is over: change it to `'X'`. 2. If an empty square `'E'` with no adjacent mines is revealed, change it to `'B'` and reveal all adjacent unrevealed squares recursively. 3. If an empty square `'E'` with at least one adjacent mine is revealed, change it to a digit `'1'` to `'8'` giving the number of adjacent mines. 4. Return the board when no more squares will be revealed.

30:00

Minesweeper
medium
Free written solution
Topics
Companies

Let's play the minesweeper game! You are given an m x n char matrix board representing the game board where:

  • 'M' is an unrevealed mine,
  • 'E' is an unrevealed empty square,
  • 'B' is a revealed blank square with no adjacent mines (above, below, left, right, and all 4 diagonals),
  • a digit '1' to '8' is a revealed square showing how many mines are adjacent to it,
  • 'X' is a revealed mine.

You are also given click = [clickr, clickc], the position of the next click among the unrevealed squares ('M' or 'E').

Return the board after revealing this position according to these rules:

  1. If a mine 'M' is revealed, the game is over: change it to 'X'.
  2. If an empty square 'E' with no adjacent mines is revealed, change it to 'B' and reveal all adjacent unrevealed squares recursively.
  3. If an empty square 'E' with at least one adjacent mine is revealed, change it to a digit '1' to '8' giving the number of adjacent mines.
  4. Return the board when no more squares will be revealed.
Example 1:
Input: {"board":[["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]],"click":[3,0]}
Output: [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
Constraints:
  • m=board.lengthm = board.length, n=board[i].lengthn = board[i].length

  • 1≤m,n≤501 \leq m, n \leq 50

  • board[i][j] is 'M', 'E', 'B', or a digit from '1' to '8'.

  • click.length == 2

  • 0≤clickr<m0 \leq clickr < m, 0≤clickc<n0 \leq clickc < n

  • board[clickr][clickc] is either 'M' or 'E'.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"board":[["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]],"click":[3,0]}

The player clicks (3, 0) on a 4 x 5 board.

•

hidden

mine (M)

popped

in queue

just changed

queue (0)

Variables
VariableValue
r3
c0
DepthFunction Call
Stack empty
0/38