[LeetCode] 130. Surrounded Regions_Medium tag: DFS

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.

这个题目思路实际上跟[LeetCode] 733. Flood Fill_Easy tag: BFS很像, 只不过我们将array遍历两边,第一次遍历边框, 如果是‘O‘ 将所有相邻的‘O‘ 都标记为visited, 然后第二次遍历

如果没有标记为visited, 将其换为‘X‘即可.

1. Constriants

1) None or n == 0

2) element will be only ‘X‘ or ‘O‘

2. Ideas

DFS/BFS     T: O(m*n)    S; O(m*n)

3. Code

class Solution:
    def surroundRegion(self, board):
        if not board or len(board[0]) == 0: return
        lr, lc , visited = len(board), len(board[0]), set()
        def dfs(r, c):
            if 0 <= r < len(board) and 0 <= c < len(board[0]):
                if (r,c) not in visited and board[r][c] == ‘O‘:
                    visited.add((r,c))
                    dfs(r+1, c)
                    dfs(r-1, c)
                    dfs(r,c+1)
                    dfs(r,c-1)

        for i in range(lr):
            for j in range(lc):
                if (i== 0 or i == lr-1 or j == 0 or j == lc -1 ) and board[i][j] == ‘O‘ and (i,j) not in visted:
                    dfs(i,j)
        for i in range(lr):
            for j in range(lc):
                if board[i][j] == ‘O‘ and (i,j) not in visited:
                    board[i][j] = ‘X‘

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9440427.html

时间: 2024-09-30 07:01:29

[LeetCode] 130. Surrounded Regions_Medium tag: DFS的相关文章

[LeetCode]130 Surrounded Regions(DFS)

题目链接:https://leetcode.com/problems/surrounded-regions/?tab=Description 题意:把非边界的O改成X. 先dfs边界的,打好标记.把没变的变成X后同时更新标记的为O. 1 class Solution { 2 public: 3 const int dx[5] = {0, 0, 1, -1}; 4 const int dy[5] = {1, -1, 0, 0}; 5 int n, m; 6 bool ok(int x, int y

Leetcode 130 Surrounded Regions DFS

将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS图所有边上的点,将边上的O以及其联通域变为T,然后将内部的O变为X,外部的T变为O,本质是种子填充算法. 此题对于is_in这个函数要注意, 不要用(x < m) && (x >= 0) && (y < n) && (y >= 0) 会堆栈溢出!!原因是会出现边上的点

leetcode 130 Surrounded Regions(BFS)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

leetcode 130. Surrounded Regions----- java

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. For example, X X X X X O O X X X O X X O X X After running your function, th

130. Surrounded Regions (Graph; DFS)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

leetcode[130] Surrounded Regions

给定一个类似棋盘,有X和O,把X圈住的O变为X例如: For 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 其实这题的思路,就是先找到四周的O,然后和四周的O相邻的O全都是不可能转化为X的,我们用另外一个符号标记,那么最后,矩阵中的O就是被圈住的,就需要转化为X,然后其他标记的符号就要转为原来的O.

Java for LeetCode 130 Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X XX O O XX X O XX O X X After running your function, the board should be:

[LeetCode] Surrounded Regions(DFS、BFS)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar