Surrounded Regions 解答

Question

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

Example:

X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X

Explanation:

Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.

Solution

基本思路是两次遍历矩阵。第一次从边界上的'O'入手,通过BFS或DFS找到所有和它连接的'O'点,把它们暂时设为另一个字符,比如'$'。第二次遍历矩阵时,剩下的'O'翻转为'X','$'翻转为'O'.

时间复杂度为O(m*n)

DFS

class Solution:
    def dfs(self, board: List[List[str]], row: int, column: int) -> None:
        m, n = len(board), len(board[0])
        if row < 0 or row >= m or column < 0 or column >= n:
            return
        if board[row][column] != 'O':
            return
        board[row][column] = '$'
        self.dfs(board, row - 1, column)
        self.dfs(board, row + 1, column)
        self.dfs(board, row, column - 1)
        self.dfs(board, row, column + 1)
    
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        if not board or not board[0]:
            return
        m, n = len(board), len(board[0])
        # DFS
        for i in range(n):
            if board[0][i] == 'O':
                self.dfs(board, 0, i)
            if board[m - 1][i] == 'O':
                self.dfs(board, m - 1, i)
        for i in range(m):
            if board[i][0] == 'O':
                self.dfs(board, i, 0)
            if board[i][n - 1] == 'O':
                self.dfs(board, i, n - 1)
        # second traverse
        for i in range(m):
            for j in range(n):
                if board[i][j] == 'O':
                    board[i][j] = 'X'
                if board[i][j] == '$':
                    board[i][j] = 'O'

BFS

from collections import deque
class Solution:
    def bfs(self, board: List[List[str]], row: int, column: int) -> None:
        m, n = len(board), len(board[0])
        if row < 0 or row >= m or column < 0 or column >= n:
            return
        if board[row][column] != 'O':
            return
        board[row][column] = '$'
        level = deque([(row, column),])
        while level:
            L = len(level)
            for i in range(L):
                curr_row, curr_column = level.popleft()
                if 0 <= curr_row - 1 < m and board[curr_row - 1][curr_column] == 'O':
                    board[curr_row - 1][curr_column] = '$'
                    level.append((curr_row - 1, curr_column))
                if 0 <= curr_row + 1 < m and board[curr_row + 1][curr_column] == 'O':
                    board[curr_row + 1][curr_column] = '$'
                    level.append((curr_row + 1, curr_column))
                if 0 <= curr_column - 1 < n and board[curr_row][curr_column - 1] == 'O':
                    board[curr_row][curr_column - 1] = '$'
                    level.append((curr_row, curr_column - 1))
                if 0 <= curr_column + 1 < n and board[curr_row][curr_column + 1] == 'O':
                    board[curr_row][curr_column + 1] = '$'
                    level.append((curr_row, curr_column + 1))
    
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        if not board or not board[0]:
            return
        m, n = len(board), len(board[0])
        # BFS
        for i in range(n):
            if board[0][i] == 'O':
                self.bfs(board, 0, i)
            if board[m - 1][i] == 'O':
                self.bfs(board, m - 1, i)
        for i in range(m):
            if board[i][0] == 'O':
                self.bfs(board, i, 0)
            if board[i][n - 1] == 'O':
                self.bfs(board, i, n - 1)
        # second traverse
        for i in range(m):
            for j in range(n):
                if board[i][j] == 'O':
                    board[i][j] = 'X'
                if board[i][j] == '$':
                    board[i][j] = 'O'

 

posted @ 2019-09-12 12:37  树獭君  阅读(316)  评论(0编辑  收藏  举报