[leetcode]Surrounded Regions @ Python

原题地址:https://oj.leetcode.com/problems/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 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

解题思路:这道题可以使用BFS和DFS两种方法来解决。DFS会超时。BFS可以AC。从边上开始搜索,如果是‘O‘,那么搜索‘O‘周围的元素,并将‘O‘置换为‘D‘,这样每条边都DFS或者BFS一遍。而内部的‘O‘是不会改变的。这样下来,没有被围住的‘O‘全都被置换成了‘D‘,被围住的‘O‘还是‘O‘,没有改变。然后遍历一遍,将‘O‘置换为‘X‘,将‘D‘置换为‘O‘。

dfs代码,因为递归深度的问题会爆栈:

class Solution:
    # @param board, a 9x9 2D array
    # Capture all regions by modifying the input board in-place.
    # Do not return any value.
    def solve(self, board):
        def dfs(x, y):
            if x<0 or x>m-1 or y<0 or y>n-1 or board[x][y]!=‘O‘:return
            board[x][y] = ‘D‘
            dfs(x-1, y)
            dfs(x+1, y)
            dfs(x, y+1)
            dfs(x, y-1)

        if len(board) == 0: return
        m = len(board); n = len(board[0])
        for i in range(m):
            dfs(i, 0); dfs(i, n-1)
        for j in range(1, n-1):
            dfs(0, j); dfs(m-1, j)
        for i in range(m):
            for j in range(n):
                if board[i][j] == ‘O‘: board[i][j] == ‘X‘
                elif board[i][j] == ‘D‘: board[i][j] == ‘O‘

bfs代码:

class Solution:
    # @param board, a 2D array
    # Capture all regions by modifying the input board in-place.
    # Do not return any value.
    def solve(self, board):
        #board最外面四条边上的‘O‘肯定不会被‘X‘包围起来, 从这些‘O‘入手, 使用BFS求出所有相邻的‘O‘, 把这些‘O‘改为另一种符号, 比如‘D‘。然后再扫描一遍board, 把‘O‘换成‘X‘, 把‘D‘换成‘O‘。

        def fill(x, y):
            if x<0 or x>m-1 or y<0 or y>n-1 or board[x][y] != ‘O‘: return
            queue.append((x,y))
            board[x][y]=‘D‘

        def bfs(x, y):
            if board[x][y]==‘O‘:  fill(x,y)
            while queue:
                curr=queue.pop(0); i=curr[0]; j=curr[1]
                fill(i+1,j);fill(i-1,j);fill(i,j+1);fill(i,j-1)

        if len(board)==0: return
        m=len(board); n=len(board[0]); queue=[]
        for i in range(n):
            bfs(0,i); bfs(m-1,i)
        for j in range(1, m-1):
            bfs(j,0); bfs(j,n-1)
        for i in range(m):
            for j in range(n):
                if board[i][j] == ‘D‘: board[i][j] = ‘O‘
                elif board[i][j] == ‘O‘: board[i][j] = ‘X‘

参考:

本文参考了[1] 但是做了个很小的修改。原文的bfs函数里面多了一个如下操作:queue.append((x,y)) 这个与fill函数的重复了。结果会导致queue每个元素有双备份。

[1] http://www.cnblogs.com/zuoyuan/p/3765434.html

时间: 2024-10-16 09:15:34

[leetcode]Surrounded Regions @ Python的相关文章

LeetCode: Surrounded Regions [130]

[题目] 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 sh

[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 Surrounded Regions 包围区域的DFS方法

在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ,一直百思不得其解其中的原因,直到有网友告诉我说他验证了最后一个大集合在本地机子上可以通过,那么我也来验证看看吧. class Solution { public: void solve(vector<vector<char> >& board) { for (int i =

LeetCode: Surrounded Regions 解题报告

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 XAfter running your function, the

LEETCODE —— Surrounded Regions

Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium 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

[LeetCode] 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 X X O O X X X O X X O X X After running your function, the board should

[LeetCode] 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 X X O O X X X O X X O X X After running your function, the board should

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

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