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(BFS)

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

  2. [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, 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. [LeetCode] 130. Surrounded Regions 包围区域

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

  5. leetcode 130. 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. Surrounded Regions

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

  7. 130. Surrounded Regions (Graph; DFS)

    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. [LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS

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

随机推荐

  1. 一、在windows环境下修改pip镜像源的方法(以python3为例)

    在windows环境下修改pip镜像源的方法(以python3为例) 1.在windows文件管理器中,输入 %APPDATA% 2.会定位到一个新的目录下,在该目录下新建pip文件夹,然后到pip文 ...

  2. thinkphp 无限极 评论

    郑创 今天用啦一天的时间用了各种方法终于把评论成无限极了,随便评论,有判断自己不能评论自己,下面先说前台源代码! 要实现的视图 前台源代码html模板 <div class="wen_ ...

  3. 操作系统中的IPC机制(inter-process Communication)

    http://www.cnblogs.com/tsiangleo/p/4902695.html

  4. [No0000147]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈4/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  5. shell脚本之分析oracle数据库数据泵日志中表的大小

    1.分析日志格式如下 . . imported "xxx_330508"."xxx_T_DATA" 46.17 MB 268 rows . . imported ...

  6. iOS 通知名的通用定义方法

    开发当中用到通知,通知的定义必须要有一个字符串标识通知的名字.一般可以直接写一个字符串,在通知创建和监听的时候直接写这个字符串. 但这样做非常不好,随手创建写代码当时很舒服,但是后来维护的时候发现通知 ...

  7. 字母算术的python算法

    据说Google出过一道题目:WWWDOT – GOOGLE = DOTCOM. 其中每个字母代表一个数字,数字不能重复,而且最高位的数字不能为0. 像这样的谜题被称为cryptarithms或者字母 ...

  8. php安全

    1.会话安全性 会话固化 一种获取有效回话标识符的方法,他将运行恶意用户通过强制使用回话ID来轻松模拟一个真实用户 攻击方法:<a href="http://a.com/index.p ...

  9. [cloud][ovs][sdn] 安装 openvswitch-dpdk

    [cloud][OVS][sdn] Open vSwitch 初步了解 继之前的内容,安装基于dpdk的ovs https://docs.openvswitch.org/en/latest/intro ...

  10. go get 的使用

    1.设置环境变量export GOPATH=/f/go_work2 2.go get github.com/xx