130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)
'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.
For 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
反向思索最简单:哪些‘O’是应该保留的?
从上下左右四个边界往里走,凡是能碰到的
‘O’ ,都是跟边界接壤的,应该保留。
思路:
对于每一个边界上的‘O’作为起点,做若干次广度
优先搜索,对于碰到的‘O’,标记为其他某字符Y;
最后遍历一遍整个地图,把所有的Y恢复成‘O’,把
所有现有的‘O’都改成‘X’
import Queue
class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
def fill(x, y):
if x < 0 or x > height-1 or y < 0 or y > width-1 or board[x][y] != "O":
return
MyQueue.put((x, y))
board[x][y] = "D" def bfs(x, y):
if board[x][y] == "O":
fill(x, y) while not MyQueue.empty():
current = MyQueue.get()
i, j = current[0], current[1]
fill(i+1, j)
fill(i-1, j)
fill(i, j+1)
fill(i, j-1) if len(board) == 0:
return height, width, MyQueue = len(board), len(board[0]), Queue.Queue()
for i in range(width):
bfs(0, i)
bfs(height - 1, i) for j in range(1, height - 1):
bfs(j, 0)
bfs(j, width - 1) for i in range(height):
for j in range(width):
if board[i][j] == "D":
board[i][j] = "O"
elif board[i][j] == "O":
board[i][j] = "X"
参考:https://www.jianshu.com/p/3ea288ffdb68
130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)的更多相关文章
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- [LintCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 130. Surrounded Regions -- 被某字符包围的区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 130 Surrounded Regions 被围绕的区域
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O), 找到所有被 'X' 围绕的区域.并将区域里所有 'O'用 'X' 填充.例如,X X X XX O O XX X O XX O X X运行你 ...
随机推荐
- sdut 面向对象程序设计上机练习十(运算符重载)
面向对象程序设计上机练习十(运算符重载) Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 定义一个复数类Complex,重载运算符"+" ...
- Linxu内核参数详解
#表示SYN队列的长度,默认为1024,加大队列长度,可以容纳更多等待连接的网络连接数. net.ipv4.tcp_max_syn_backlog = 65536 #每个网络接口接收数据包的速率比内核 ...
- WebApi(6) 后台C#调用WebApi
https://www.cnblogs.com/cxd1008/p/6640015.html 今天来写一下后台C#代码如何访问webapi 这里使用HttpClient方法访问webapi也是很常用的 ...
- ArcGIS 空间数据库备份
Expdp system/ddd@orcl directory=sdebak dumpfile=20170124_1_ServerBak.dmp schemas=(sde, ddd)
- 将list列表中unicode类型的值转换为字符串类型
- 应用开发之Linq和EF
本章简言 上一章笔者对于WinForm开发过程用到的几个知识点做了讲解.笔者们可以以此为开端进行学习.而本章我们来讲一个跟ORM思想有关的知识点.在讲之前让我们想一下关于JAVA的hibernate知 ...
- Android无线测试之—UiAutomator UiWatcher API介绍一
UiWatcher类介绍与中断监听检查条件 一.UiWatcher类说明 1.Uiwatcher用于处理脚本执行过程中遇到非预想的步骤 2.UiWatcher使用场景 1)测试过程中来了一个电话 2) ...
- 缺陷管理工具JIRA破解版及其安装方法
JIRA是一个优秀的问题(or bugs,task,improvement,new feature )跟踪及管理软件. 它由Atlassian开发,采用J2EE技术.它正被广泛的开源软件组织,以 ...
- hdu 1300(dp)
一个模式的dp. Pearls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- spring配置文件注解方式引入的两种方式
一.#{beanID['propertiesName']}方式 <bean id="propertyConfigurer" class="org.springfra ...