【LeetCode OJ】Surrounded Regions
Problem Link:
http://oj.leetcode.com/problems/surrounded-regions/
We can do follows in the 2D board.
Use BFS from all 'O' cells on the boundary and mark all connected 'O' cells
Scan the board and flip all unmarked 'O' to 'X'.
The following code is python code accepted by oj.leetcode.com.
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):
"""
Let m and n be the number of rows and columns of board
BFS from 'O' nodes on the boundary
"""
# Special case 1: if m <= 2, then all cells are on the boundary
m = len(board)
if m <= 2:
return
# Special case 2: if n <= 2, then all cells are on the boundary
n = len(board[0])
if n <= 2:
return # Queue used for BFS
Q = [] # Find all 'O' cells in on the boundary
# check the first row and last row
for j in xrange(n):
if board[0][j] == 'O':
Q.append( (0,j) )
if board[m-1][j] == 'O':
Q.append( (m-1,j) )
# check the first column and last column
for i in xrange(1,m-1):
if board[i][0] == 'O':
Q.append( (i,0) )
if board[i][n-1] == 'O':
Q.append( (i,n-1) ) # Start BFS from the nodes in Q
# Each time set the node to "M",
# which means the node is not captured
while Q:
new_Q = []
for (i,j) in Q:
board[i][j] = 'M'
# Check left node
if i > 0 and board[i-1][j] == 'O':
new_Q.append( (i-1,j) )
# Check right node
if i < m-1 and board[i+1][j] == 'O':
new_Q.append( (i+1,j) )
# Check top node
if j > 0 and board[i][j-1] == 'O':
new_Q.append( (i, j-1) )
# Check bottom node
if j < n-1 and board[i][j+1] == 'O':
new_Q.append( (i, j+1) )
Q = new_Q # Scan the 2D board
# 1) set node of O to X
# 2) set node of OO to O
for i in xrange(m):
for j in xrange(n):
if board[i][j] == 'O':
board[i][j] = 'X'
if board[i][j] == 'M':
board[i][j] = 'O'
【LeetCode OJ】Surrounded Regions的更多相关文章
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
随机推荐
- HTML5自学笔记[ 6 ]data自定义数据
在标签中添加data-name属性并赋值,在js脚本中用ele.dataset.name就可以获取该属性的值.如: <div id="box" data-age=" ...
- Compound Interest Calculator4.0
Compound Interest Calculator4.0 1.团队协作准备:每个同学在github上完成FORK,COMMENT(学号后三位+姓名),PR,MERGE的过程. 2.你的RP由你的 ...
- hdu 4033Regular Polygon(二分+余弦定理)
Regular Polygon Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)T ...
- AngularJS理论基础
AngularJS理论基础 AngularJs是一个用于设计动态web应用的结构框架. 它是一个框架,不是类库,是像EXT一样提供一整套方案用于设计web应用.它不仅仅是一个javascript框架, ...
- hadoop工作流引擎之azkaban [转]
介绍 Azkaban是twitter出的一个任务调度系统,操作比Oozie要简单很多而且非常直观,提供的功能比较简单.Azkaban以Flow为执行单元进行定时调度,Flow就是预定义好的由一个或多个 ...
- 使用Web Service进行网络编程-----Web Service简介
Android应用通常都是运行在手机平台上,手机系统的硬件资源是有限的,不管是存储能力还是计算能力都是有限的,在Android系统上开发.运行一些单用户.小型应用是可能的,但对于需要进行大量的数据处理 ...
- uva 1428
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 如何实现ASP.NET中网站访问量的统计
如何实现ASP.NET中网站访问量的统计 2009-07-30 15:50 佚名 网翼教程网 字号:T | T 本文介绍了如何在asp.net中进行网站访问量的统计. AD:51CTO 网+ 第十二期 ...
- sql 查询强制使用HASH连接性能测试比较
HASH JOIN 散列连接 hash join是CBO 做大数据集连接时的常用方式.优化器扫描小表(或数据源),利用连接键(也就是根据连接字段计算hash 值)在内存中建立hash表,然后扫描大表, ...
- S1:对象与JSON
JSON全称为JavaScript对象表示法(JavaScript Object Notation). JSON是JavaScript中对象的字面量,是对象的表示方法,通过使用JSON,可以减少中间变 ...