Leetcode Surrounded Regions(Medium)-python

Leetcode Surrounded Regions(Medium)

问题描述:

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

将被X包围的O全部变成X,边界上的O不算被包围。

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.

算法一(DFS):

看了grandyang大神的博客后,写的python版本。

DFS就是先从头开始遍历四个边,如果遇到O就调用dfs函数,把这个位置变成W,这样遍历结束后,矩阵中与边界上O相邻的O都变成了W,然后我再遍历一次,把W变成O(因为她没被X围住),把O变成X。

class Solution:
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        def dfs(board,i,j):
            board[i][j] = 'W'
            if i > 0 and board[i - 1][j] == 'O':
                dfs(board,i - 1,j)
            if j > 0 and board[i][j - 1] == 'O':
                dfs(board,i,j - 1)
            if i < len(board) - 1 and board[i + 1][j] == 'O':
                dfs(board,i + 1,j)
            if j < len(board[i]) - 1 and board[i][j + 1] == 'O':
                dfs(board,i,j + 1)
        for i in range(len(board)):
            for j in range(len(board[i])):
                if (i == 0 or i == len(board) - 1 or j == 0 or j == len(board[i]) - 1) and board[i][j] == 'O':
                    dfs(board,i,j)
        for i in range(len(board)):
            for j in range(len(board[i])):
                if board[i][j] == 'O':
                    board[i][j] = 'X'
                if board[i][j] == 'W':
                    board[i][j] = 'O'

算法二(BFS):

class Solution:
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """

        for i in range(len(board)):
            for j in range(len(board[i])):
                if (i == 0 or i == len(board) - 1 or j == 0 or j == len(board[i]) - 1) and board[i][j] == 'O':
                    q = [[i,j]]
                    while q:
                        m,n = q.pop()
                        board[m][n] = 'W'
                        if m > 0 and board[m - 1][n] == 'O':
                            q.append([m - 1,n])
                        if n > 0 and board[m][n - 1] == 'O':
                            q.append([m,n - 1])
                        if m < len(board) - 1 and board[m + 1][n] == 'O':
                            q.append([m + 1,n])
                        if n < len(board[m]) - 1 and board[m][n + 1] == 'O':
                            q.append([m,n + 1])
        for i in range(len(board)):
            for j in range(len(board[i])):
                if board[i][j] == 'O':
                    board[i][j] = 'X'
                if board[i][j] == 'W':
                    board[i][j] = 'O'

你可能感兴趣的:(Leetcode Surrounded Regions(Medium)-python)