477. 被围绕的区域

Posted yunxintryyoubest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了477. 被围绕的区域相关的知识,希望对你有一定的参考价值。

477. 被围绕的区域

中文English

给一个二维的矩阵,包含 ‘X‘ 和 ‘O‘, 找到所有被 ‘X‘ 围绕的区域,并用 ‘X‘ 替换其中所有的 ‘O‘

样例

样例 1:

输入:
  X X X X
  X O O X
  X X O X
  X O X X
输出: 
  X X X X
  X X X X
  X X X X
  X O X X

样例 2:

输入: 
  X X X X
  X O O X
  X O O X
  X O X X
输出: 
  X X X X
  X O O X
  X O O X
  X O X X
输入测试数据 (每行一个参数)如何理解测试数据?

 DFS + 边界寻找 O 替换 T

O替换X,T替换O

class Solution:
    """
    @param: board: board a 2D board containing X and O
    @return: nothing
    """
    def surroundedRegions(self, board):
        # write your code here
        if not board: return


        #初始化
        n, m = len(board), len(board[0])

        def dfs(x, y):
            if (x < 0 or x > n - 1 or y < 0 or y > m - 1):
                return
            
            if board[x][y] == O:
                board[x][y] = T
            
                dfs(x - 1, y)
                dfs(x + 1, y)
                dfs(x, y - 1)
                dfs(x, y + 1)


        #找寻边界,如果满足边界 and 为0,则dfs找到上下左右的,换T
        #此时j为0和m - 1
        for i in range(n):
            if (board[i][0] == O):
                dfs(i, 0)
            if (board[i][m - 1] == O):
                dfs(i, m - 1)
        
        for j in range(m):
            if (board[0][j] == O):
                dfs(0, j)
            if (board[n - 1][j] == O):
                dfs(n - 1, j)
            
        
        #然后循环m和n,依次将O转换为X,并且T转换为O
        for i in range(n):
            for j in range(m):
                if (board[i][j] == O):
                    #board[i] = board[i].replace(board[i][j], X, 1)
                    board[i][j] = X
                elif (board[i][j] == T):
                    board[i][j] = O
                    #board[i] = board[i].replace(board[i][j], O, 1)

        return board

 

BFS写法 + 边界查找 替换法

class Solution:
    """
    @param: board: board a 2D board containing X and O
    @return: nothing
    """
    def surroundedRegions(self, board):
        # write your code here
        #BFS写法,每次只fill一次,查询一次,符合则替换,最终返回结果
        if not board: return
    
        m, n = len(board[0]), len(board)
        queue = []
                
        def fill(x, y):
            if (x < 0 or x > n - 1 or y < 0 or y > m - 1):
                return
            
            if (board[x][y] == O):
                board[x][y] = T
                queue.append([x, y])
                
        
        def bfs(x ,y):
            fill(x, y)
        
            while queue:
                curr = queue.pop(0)
                x, y = curr[0], curr[1]
                
                fill(x + 1, y)
                fill(x - 1, y)
                fill(x, y + 1)
                fill(x, y - 1)
                

        #边界查找
        #X轴查找
        for i in range(n):
            if (board[i][0] == O):
                bfs(i ,0)
            if (board[i][m - 1] == O):
                bfs(i, m - 1)
                
        #Y轴查找
        for j in range(m):
            if (board[0][j] == O):
                bfs(0, j)
            if (board[n - 1][j] == O):
                bfs(n - 1, j)
        
        #最终替换
        for i in range(m):
            for j in range(n):
                if (board[j][i] == O):
                    board[j][i] = X
                elif (board[j][i] == T):
                    board[j][i] = O
        
        return board
                

 

以上是关于477. 被围绕的区域的主要内容,如果未能解决你的问题,请参考以下文章

java刷题--130被围绕的区域

130被围绕的区域

130被围绕的区域

LeetCode第130题—被围绕的区域—Python实现

LeetCode 0130.被围绕的区域 - BFS:标记没有被围绕的区域

lintcode:被围绕的区域