【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 ...
随机推荐
- 错误:Error:未定义标识符"_TCHAR"
原因:缺少头文件 解决方案:添加一条 #include <tchar.h>
- Android基础之项目结构分析
创建了第一个Android项目,用工具开发Android项目,我们有必要熟悉项目的目录结构,清楚各个项目下面放置的是什么东西.展开整个项目,其根目录结构(选用不同版本的SDK文件目录结构会有一些不同, ...
- Hdu4349 Xiao Ming's Hope
Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- PHP+socket游戏数据统计平台发包接包类库
<?php /** * @title: PHP+socket游戏数据统计平台发包接包类库 * @version: 1.0 * @author: perry <perry@1kyou.com ...
- EF的Model First
一,添加ADO.NET实体数据模型(即edmx) 1,添加edmx 新建一个类库项目,项目中添加新项,选择数据/ADO.NET实体数据模型,如下图. 点击添加,实体数据模型向导窗口 ...
- submit回车提交影响
$(".bInput").bind('keydown',function(event){//回车提交手动标签 if(event.keyCode==13){ ...
- bzoj 2440: [中山市选2011]完全平方数
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #defin ...
- Oracle 11g 安装
1.Oracle 11g安装: http://www.cnblogs.com/qianyaoyuan/archive/2013/05/05/3060471.html 2.安装完Oracle数据库,给s ...
- 关于Tcp,为什么一定要进行三次握手呢?
主要是防止已经失效的请求报文段突然又传送到了服务端而产生的连接的误判. 考虑如下的情况:客户端发送了一个连接请求报文段到服务端,但是在某些网络节点上长时间滞留了,而后客户端又超时重发了一个连接请求报文 ...