Practice/Microsoft/Leetcode 529. Minesweeper
CodingMust
You are implementing the click behavior for a mine-sweeper style game. Given a 2D character grid representing the game board and a click position, simulate what happens after the click.
The board uses these characters:
'M' represents an unrevealed mine'E' represents an unrevealed empty square'B' represents a revealed blank square (no adjacent mines)'1' to '8' represent revealed squares showing the count of adjacent mines'X' represents a revealed mineWhen a square is clicked:
If it contains a mine ('M'), reveal it by changing it to 'X'. The game ends here.
If it's an empty square ('E'):
'B' and recursively reveal all 8 adjacent unrevealed empty squaresIf the square is already revealed (not 'E' or 'M'), do nothing
Return the modified board after processing the click.
1 <= board.length, board[i].length <= 50board[i][j] is one of: 'M', 'E', 'B', or a digit '1'-'8'click is a valid position within the board boundsclick will always point to an unrevealed cell ('M' or 'E')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 = [1, 2]
Output: [['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'X', 'E', 'E'], ['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E']]
Explanation: Clicking on a mine reveals it as 'X'. `
Example 2:
` 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']]
Explanation: Clicking on a cell with no adjacent mines triggers a flood-fill that recursively reveals all safe connected cells. Cells adjacent to the mine show their mine count. `