讨论/《广度优先搜索》 - 练习:扫雷游戏/
《广度优先搜索》 - 练习:扫雷游戏
共 1 个回复
class Solution:
    def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
        x, y = click
        row, col = len(board), len(board[0])
        
        if board[x][y] == 'M': # 挖出了雷
            board[x][y] = 'X'
            return board
        cnt = self.countMine(board, x, y, row, col)
        
        if cnt > 0: # 相邻***
            board[x][y] = str(cnt)
            return board 
        
        # 不相邻***
        board[x][y] = 'B'
        for i in range(x-1, x+2):
            for j in range(y-1, y+2):
                if i == x and j == y:
                    continue 
                if self.inArea(i, j, row, col) and board[i][j] == 'E':
                    board = self.updateBoard(board, [i, j])
        return board
        
    def countMine(self, board, x, y, row, col):
        ans = 0
        for i in range(x-1, x+2):
            for j in range(y-1, y+2):
                if i == x and j == y:
                    continue 
                if self.inArea(i, j, row, col) and board[i][j] == 'M':
                    ans += 1
        return ans 
                
    def inArea(self, x, y, row, col):
        return 0 <= x < row and 0 <= y < col