Python Clear Solution


  • 0
    D
    class Solution(object):
        def updateBoard(self, board, click):
            """
            :type board: List[List[str]]
            :type click: List[int]
            :rtype: List[List[str]]
            """
            r, c = click[0], click[1]
            if board[r][c] == 'M':
                 board[r][c] = 'X'
                 return board
                 
            m, n = len(board), len(board[0])
            
            def numMines(row, col):
                return sum(row+x[0]>= 0 and row+x[0] < m and col+x[1]>= 0 and col+x[1] < n and  board[row+x[0]][col + x[1]] == 'M' for x in [(-1, 0), (1, 0), (-1, 1), (-1, -1), (1, 1), (1, -1), (0, 1), (0, -1)])
            
            def recurse(row, col):
                if board[row][col] != 'E': return
                nMine = numMines(row, col)
                if nMine == 0:
                    board[row][col] = 'B'
                    for x in [(-1, 0), (1, 0), (-1, 1), (-1, -1), (1, 1), (1, -1), (0, 1), (0, -1)]:
                        if row+x[0]>= 0 and row+x[0] < m and col+x[1]>= 0 and col+x[1] < n: recurse(row+x[0], col+x[1])
                else:
                    board[row][col] = str(nMine)
            
            recurse(r, c)
            return board

Log in to reply
 

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