PU Minesweeper

Jan 01, 1970

Let's play the minesweeper game (Wikipedia, online game)!

You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.

Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:

  • If a mine ('M') is revealed, then the game is over - change it to 'X'.
  • If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively.
  • If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
  • Return the board when no more squares will be revealed.

Example 1:

  • Input:
[['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:
    Minesweeper-1

Example 2:

  • Input:
[['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']]
  • Explanation:
    Minesweeper-2

Note:

  • The range of the input matrix's height and width is [1,50].
  • The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
  • The input board won't be a stage when game is over (some mines have been revealed).
  • For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.

Python Solution 1:

class Solution(object):
    def updateBoard(self, board, click):
        """
        :type board: List[List[str]]
        :type click: List[int]
        :rtype: List[List[str]]
        """
        res = [row[:] for row in board]
        rlen = len(board)
        clen = len(board[0])
        def recu(board, click):
            row = click[0]
            col = click[1]
            if board[row][col] == 'M':
                board[row][col] = 'X'
                return
            num = 0
            num += int(row < rlen - 1 and board[row + 1][col] == 'M')
            num += int(0 < row and board[row - 1][col] == 'M')
            num += int(col < clen - 1 and board[row][col + 1] == 'M')
            num += int(0 < col and board[row][col - 1] == 'M')
            num += int(row > 0 and col > 0 and board[row - 1][col - 1] == 'M')
            num += int(row > 0 and col < clen - 1 and board[row - 1][col + 1] == 'M')
            num += int(row < rlen - 1 and col > 0 and board[row + 1][col - 1] == 'M')
            num += int(row < rlen - 1 and col < clen - 1 and board[row + 1][col + 1] == 'M')
            if num:
                board[row][col] = str(num)
                return
            board[row][col] = 'B'
            if row < rlen - 1 and board[row + 1][col] == 'E':
                recu(board, [row + 1, col])
            if 0 < row and board[row - 1][col] == 'E':
                recu(board, [row - 1, col])
            if col < clen - 1 and board[row][col + 1] == 'E':
                recu(board, [row, col + 1])
            if 0 < col and board[row][col - 1] == 'E':
                recu(board, [row, col - 1])
            if row > 0 and col > 0 and board[row - 1][col - 1] == 'E':
                recu(board, [row - 1, col - 1])
            if row > 0 and col < clen - 1 and board[row - 1][col + 1] == 'E':
                recu(board, [row - 1, col + 1])
            if row < rlen - 1 and col > 0 and board[row + 1][col - 1] == 'E':
                recu(board, [row + 1, col - 1])
            if row < rlen - 1 and col < clen - 1 and board[row + 1][col + 1] == 'E':
                recu(board, [row + 1, col + 1])
        recu(res, click)
        return res

Python Solution 2:

class Solution(object):
    def updateBoard(self, board, click):
        """
        :type board: List[List[str]]
        :type click: List[int]
        :rtype: List[List[str]]
        """
        R = len(board)
        C = len(board[0])
        r, c = click
        if r < 0 or r > R - 1 or c < 0 or c > C - 1:
            return board
        if board[r][c] != 'M' and board[r][c] != 'E': 
            return board
        if board[r][c] == 'M':
            board[r][c] = 'X'
            return board
        if board[r][c] == 'E':
            mines = 0
            if r > 0 and board[r - 1][c] == 'M':
                mines += 1
            if r > 0 and c > 0 and board[r - 1][c - 1] == 'M':
                mines += 1
            if c > 0 and board[r][c - 1] == 'M':
                mines += 1
            if c > 0 and r < R - 1 and board[r + 1][c - 1] == 'M':
                mines += 1
            if r < R - 1 and board[r + 1][c] == 'M':
                mines += 1
            if r < R - 1 and c < C - 1 and board[r + 1][c + 1] == 'M':
                mines += 1
            if c < C - 1 and board[r][c + 1] == 'M':
                mines += 1
            if r > 0 and c < C - 1 and board[r - 1][c + 1] == 'M':
                mines += 1
            if mines:
                board[r][c] = str(mines)
                return board
            board[r][c] = 'B'
            self.updateBoard(board, [r - 1, c])
            self.updateBoard(board, [r - 1, c - 1])
            self.updateBoard(board, [r, c- 1])
            self.updateBoard(board, [r + 1, c - 1])
            self.updateBoard(board, [r + 1, c])
            self.updateBoard(board, [r + 1, c + 1])
            self.updateBoard(board, [r, c + 1])
            self.updateBoard(board, [r - 1, c + 1])
        return board

Python Solution 3:

class Solution(object):
    def updateBoard(self, board, click):
        """
        :type board: List[List[str]]
        :type click: List[int]
        :rtype: List[List[str]]
        """
        r, c = click
        R, C = len(board), len(board[0])
        if board[r][c] == 'M':
            board[r][c] = 'X'
            return board
        mines = 0
        for i in range(-1, 2):
            for j in range(-1, 2):
                if not i and not j: continue
                _r = r + i
                _c = c + j
                if _r < 0 or _r > R - 1 or _c < 0 or _c > C - 1: continue
                if board[_r][_c] == 'M':
                    mines += 1
        if mines:
            board[r][c] = str(mines)
            return board
        board[r][c] = 'B'
        for i in range(-1, 2):
            for j in range(-1, 2):
                if not i and not j: continue
                _r = r + i
                _c = c + j
                if _r < 0 or _r > R - 1 or _c < 0 or _c > C - 1: continue
                if board[_r][_c] == 'E':
                    self.updateBoard(board, [_r, _c])
        return board

Summary:

  • The most difficult part of this problem is to understand the logic.

LeetCode: 529. Minesweeper