#529

Minesweeper

specialist · 625 · lc medium +29 · verified · 68.7% accepted · 2,084 likes · top 76%

play →

Description

You are playing Minesweeper. The m x n board uses these characters:

- 'M': unrevealed mine

- 'E': unrevealed empty cell

- 'B': revealed blank (no mines among its 8 neighbors)

- '1''8': revealed cell showing the adjacent mine count

- 'X': revealed mine

Given a click = [clickr, clickc] on an unrevealed cell, update and return the board using these rules:

- Clicking a mine 'M' ends the game; mark it 'X'.

- Clicking an empty cell 'E' with no adjacent mines marks it 'B' and recursively reveals all neighboring unrevealed cells.

- Clicking an empty cell 'E' with at least one adjacent mine marks it with the adjacent mine count digit.

- Stop when no further cells need revealing.

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"]]

Example 2:

Input: board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
Output: [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]

Code

1
2
3