Simple python


  • 0
    class Solution(object):
        def updateBoard(self, board, click):
            """
            :type board: List[List[str]]
            :type click: List[int]
            :rtype: List[List[str]]
            """
            if board[click[0]][click[1]] == "M":
                board[click[0]][click[1]] = "X"
            elif board[click[0]][click[1]] == "E":
                self.helper(board, click[0], click[1])
            return board
            
        def helper(self, board, row, column):
            if board[row][column] == "E":
                mine = self.mineAround(board, row, column)
                board[row][column] = str(mine) if mine > 0 else "B"
                if mine == 0:
                    if row - 1 >= 0 and board[row - 1][column] == "E":
                        self.helper(board, row - 1, column)
                    if row - 1 >= 0 and column - 1 >= 0 and board[row - 1][column - 1] == "E":
                        self.helper(board, row - 1, column - 1)
                    if row - 1 >= 0 and column + 1 < len(board[row]) and board[row - 1][column + 1] == "E":
                        self.helper(board, row - 1, column + 1)
                    if column - 1 >= 0 and board[row][column - 1] == "E":
                        self.helper(board, row, column - 1)
                    if column + 1 < len(board[row]) and board[row][column + 1] == "E":
                        self.helper(board, row, column + 1)
                    if row + 1 < len(board) and board[row + 1][column] == "E":
                        self.helper(board, row + 1, column)
                    if row + 1 < len(board) and column - 1 >= 0 and board[row + 1][column - 1] == "E":
                        self.helper(board, row + 1, column - 1)
                    if row + 1 < len(board) and column + 1 < len(board[row]) and board[row + 1][column + 1] == "E":
                        self.helper(board, row + 1, column + 1)
            
        def mineAround(self, board, row, column):
            count = 0
            if row - 1 >= 0 and board[row - 1][column] == "M":
                count += 1
            if row - 1 >= 0 and column - 1 >= 0 and board[row - 1][column - 1] == "M":
                count += 1
            if row - 1 >= 0 and column + 1 < len(board[row]) and board[row - 1][column + 1] == "M":
                count += 1
            if column - 1 >= 0 and board[row][column - 1] == "M":
                count += 1
            if column + 1 < len(board[row]) and board[row][column + 1] == "M":
                count += 1
            if row + 1 < len(board) and board[row + 1][column] == "M":
                count += 1
            if row + 1 < len(board) and column - 1 >= 0 and board[row + 1][column - 1] == "M":
                count += 1
            if row + 1 < len(board) and column + 1 < len(board[row]) and board[row + 1][column + 1] == "M":
                count += 1
            return count

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.