【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 ...
随机推荐
- hdu---(1800)Flying to the Mars(trie树)
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 使用git上传项目
1. 安装Git 2. 安装TortoiseGit 3.任意文件夹选择「TortoiseGit」>「settings」,打开如下界面. 3. 生成SSH公钥 3.1运行Git Bash,如下命令 ...
- Python eclipse开发环境搭建
http://jingyan.baidu.com/article/cd4c2979101f02756f6e6064.html http://jingyan.baidu.com/article/1876 ...
- 前端面试题之nina分享
HTML相关 1.<!DOCTYPE>标签的定义与用法. <!DOCTYPE>的定义: <!DOCTYPE>声明位于文档中的最前面的位置,处于<html> ...
- Collecting Bugs(POJ 2096)
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 3064 Accepted: 1505 ...
- Github注册流程和使用体验
大家好,我叫施蓓蓓,学号1413042063,在网络工程143班,我的兴趣爱好有很多,特别是在专业方面,比如软件工程.操作系统.网络通信技术.计算机组成原理等,我对游戏十分感兴趣,以后就业会朝这方面发 ...
- Hadoop集群中添加硬盘
Hadoop工作节点扩展硬盘空间 接到老板任务,Hadoop集群中硬盘空间不够用,要求加一台机器到Hadoop集群,并且每台机器在原有基础上加一块2T硬盘,老板给力啊,哈哈. 这些我把完成这项任务的步 ...
- WCF服务编程中使用SvcMap实现类型共享等技巧【转】
原文链接:http://www.cr173.com/html/19026_1.html 国外的一篇文章:Sharing DataContracts between WCF Services 文中提到的 ...
- SQL查询表占用空间大小
SQL查询表占用空间大小. create table tmp (name varchar(50),rows int,reserved varchar(50),data varchar(50),inde ...
- 高效前端优化工具--Fiddler入门教程
简介: Fiddler是用C#编写的一个免费的HTTP/HTTPS网络调试器.Fiddler是以代理服务器的方式,监听系统的网络数据流动英语中Fiddler是小提琴的意思,Fiddler Web De ...