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运行你 ...
随机推荐
- digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04
https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-16-04 Intr ...
- VC++分页打印实现
VC++分页打印实现: C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243 ...
- mybatis总结(三)之多表查询
上一节,已经把实体类和配置文件都写过了,这节课直接添加几个方法吧 在DeptMapper.xml文件中添加 <!-- 多表查询(1对多) ,通过部门编号,查询出部门所在的员工姓名,部门名,部门编 ...
- 编程之美 set 19 连连看游戏设计
题目 1. 怎么用简单的计算机模型来描述这个问题 ? 2. 怎么判断两个图像是否能相消 3. 怎么求出相同图形之间的最短距离(转弯数最少)? 4. 怎么确定目前处于死锁状态? 如何设计算法来解除死锁? ...
- MVC框架图
http://www.cnblogs.com/zgynhqf/archive/2010/11/19/1881449.html MVC框架图 http://www.cnblogs.com/zhang ...
- Unable to acquire Oracle environment handle 问题的解决
---恢复内容开始--- 转自:http://blog.csdn.net/zhangweiwindow/article/details/6575224 今天重装了一下系统,所以以前工作时配置的pyth ...
- android最新版 极光推送
极光推送对于移动开发的程序员都不陌生,用起来也挺方便的,今天在这里给大家介绍下最先版的极光推送的用法,超级简单. 1.在build.gradle里面添加两个方法并引用一个库文件 1.1在default ...
- kafka 相关配置
kafka主要配置包括三类:broker configuration,producer configuration and consumer configuration. Broker Config ...
- 动态svg效果
import React from 'react'; import TweenOne from 'rc-tween-one'; import SvgDrawPlugin from 'rc-tween- ...
- Struts入门(三)深入Struts用法讲解
访问Servlet API Action搜索顺序 动态方法调用 指定多个配置文件 默认Action Struts 后缀 接收参数 处理结果类型 1.访问Servlet API 首先我们了解什么是Ser ...