[LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

时间:2022-09-07 08:04:38

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

3.1) DFS

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'

3.2) BFS

class Solution:
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
if not board or len(board[0]) == 0 : return
lr, lc, queue, visited = len(board), len(board[0]), collections.deque(), set()
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' :
queue.append((i,j))
visited.add((i,j))
dirs = [(0,1), (0,-1), (1,0), (-1,0)]
while queue:
pr, pc = queue.popleft()
for c1, c2 in dirs:
nr, nc = pr + c1 , pc + c2
if 0 <= nr < lr and 0 <= nc <lc and (nr, nc) not in visited and board[nr][nc] == 'O':
queue.append((nr, nc))
visited.add((nr, nc)) for i in range(lr):
for j in range(lc):
if board[i][j] == 'O' and (i,j) not in visited: # first check (i, j) not in visited will speed up the process a lot.
board[i][j] = 'X'

[LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS的更多相关文章

  1. leetcode 130 Surrounded Regions&lpar;BFS&rpar;

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. &lbrack;LeetCode&rsqb; 785&period; Is Graph Bipartite&quest;&lowbar;Medium tag&colon; DFS&comma; BFS

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  3. 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的基本框架是 void dfs ...

  4. &lbrack;LeetCode&rsqb; 130&period; Surrounded Regions 包围区域

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

  5. leetcode 130&period; Surrounded Regions----- java

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

  6. Leetcode 130&period; Surrounded Regions

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

  7. 130&period; Surrounded Regions &lpar;Graph&semi; DFS&rpar;

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  8. Java for LeetCode 130 Surrounded Regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  9. &lbrack;LeetCode&rsqb; 872&period; Leaf-Similar Trees&lowbar;Easy tag&colon; DFS

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

随机推荐

  1. CSS3&colon;动画大全

    和过渡的区别 页面不用明显js调用: 过渡:必须有:hover visited 等伪类调用.(本质还是事件驱动) 动画:页面加载上就可以. 页面有js调用: 7个参数,*为可选 animation-n ...

  2. Android WebView中的JavaScript代码使用

    在WebView中使用JavaScript 如果你想要载入的页面中用了JavaScript,你必须为你的WebView使能JavaScript. 一旦使能之后,你也可以自己创建接口在你的应用和Java ...

  3. ClassLoader&comma; JavaAgent&comma; Aspectj Weaving一站式扫盲帖

    最近工作里复习的Class Loader基础知识集锦,写下来希望对别人有帮助,而且不止是为了撂倒面试官. 为了尽量简单明了容易背,有些部分写得比较干. 0. 参考资料: 书:<深入了解Java虚 ...

  4. Android开发&lowbar;关于如何屏蔽Home键

    今天在遇到一个要屏蔽Home键的问题,研究一上午终于解决,方法记录于下: 在Android2.3版本以下重写以下方法就能屏蔽Home键: public void onAttachedToWindow( ...

  5. 地图API地址  百度地图开放平台

    http://lbsyun.baidu.com/index.php?title=jspopular

  6. Python简介之探观止矣

    Python是一门什么样的编程语言编程语言主要分为编译型和解释型,静态语言和动态语言,强类型和弱类型,混合语言等.编译型语言:通过编译器把源代码编译(compile)成机器语言,在经过链接(linke ...

  7. 【转】Tomcat 快速入门

    本文转载自:https://www.cnblogs.com/jingmoxukong/p/8258837.html?utm_source=gold_browser_extension 目录 Tomca ...

  8. 2018&OpenCurlyDoubleQuote;金三”之一线互联网公司Java高级面试题总结

    JVM 1.请介绍一下JVM内存模型??用过什么垃圾回收器都说说呗 2.线上发送频繁full gc如何处理? CPU 使用率过高怎么办? 如何定位问题?如何解决说一下解决思路和处理方法 3.知道字节码 ...

  9. python 将元组解析为多个参数

    #create a tuple tuplex = , , print(tuplex) n1, n2, n3 = tuplex #unpack a tuple in variables print(n1 ...

  10. HDU 3339 In Action(最短路&plus;背包)题解

    思路:最短路求出到每个点的最小代价,然后01背包,求出某一代价所能拿到的最大价值,然后搜索最后结果. 代码: #include<cstdio> #include<set> #i ...