[leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/
题意:
Given a 2D board containing 'X'
and '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
解题思路:这道题可以使用BFS和DFS两种方法来解决。DFS会超时。BFS可以AC。从边上开始搜索,如果是'O',那么搜索'O'周围的元素,并将'O'置换为'D',这样每条边都DFS或者BFS一遍。而内部的'O'是不会改变的。这样下来,没有被围住的'O'全都被置换成了'D',被围住的'O'还是'O',没有改变。然后遍历一遍,将'O'置换为'X',将'D'置换为'O'。
dfs代码,因为递归深度的问题会爆栈:
class Solution:
# @param board, a 9x9 2D array
# Capture all regions by modifying the input board in-place.
# Do not return any value.
def solve(self, board):
def dfs(x, y):
if x< or x>m- or y< or y>n- or board[x][y]!='O':return
board[x][y] = 'D'
dfs(x-, y)
dfs(x+, y)
dfs(x, y+)
dfs(x, y-) if len(board) == : return
m = len(board); n = len(board[])
for i in range(m):
dfs(i, ); dfs(i, n-)
for j in range(, n-):
dfs(, j); dfs(m-, j)
for i in range(m):
for j in range(n):
if board[i][j] == 'O': board[i][j] == 'X'
elif board[i][j] == 'D': board[i][j] == 'O'
bfs代码:
class Solution:
# @param board, a 9x9 2D array
# Capture all regions by modifying the input board in-place.
# Do not return any value.
def solve(self, board):
def fill(x, y):
if x< or x>m- or y< or y>n- or board[x][y] != 'O': return
queue.append((x,y))
board[x][y]='D'
def bfs(x, y):
if board[x][y]=='O':queue.append((x,y)); fill(x,y)
while queue:
curr=queue.pop(); i=curr[]; j=curr[]
fill(i+,j);fill(i-,j);fill(i,j+);fill(i,j-)
if len(board)==: return
m=len(board); n=len(board[]); queue=[]
for i in range(n):
bfs(,i); bfs(m-,i)
for j in range(, m-):
bfs(j,); bfs(j,n-)
for i in range(m):
for j in range(n):
if board[i][j] == 'D': board[i][j] = 'O'
elif board[i][j] == 'O': board[i][j] = 'X'
[leetcode]Surrounded Regions @ Python的更多相关文章
- [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: Surrounded Regions 解题报告
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Leetcode: Surrounded regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- LEETCODE —— Surrounded Regions
Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- [LeetCode] Surrounded Regions 广度搜索
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- 如何用dat批处理文件关闭某端口对应程序-Windows自动化命令
如何用dat批处理文件关闭某端口对应程序? 网上找到的大部分都是手动操作,第一步先查出端口,第二步在根据上一步查到的端口手动去关闭进程.但我的需求不是这样的,我需要全自动处理.用于 dubbo 服务进 ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- hdu 5774 Where Amazing Happens 水题
Where Amazing Happens 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5774 Description As the premie ...
- FireDAC 下的 Sqlite [1] - 前言
很长时间没静下心来写博客了, 现在回来, 是 Delphi 不断地进步让我感动.振奋. Delphi XE5 并入了 FireDAC, 第一印象非常好, 恐怕 dbExpress 等等都要靠边站了. ...
- CentOS 7解压安装PHP5.6.13
自动化脚本: https://github.com/easonjim/centos-shell/blob/master/php/install-php_5.6.13.sh
- 用 consul + consul-template + registrator + nginx 打造真正可动态扩展的服务架构
https://mp.weixin.qq.com/s?src=3×tamp=1503654544&ver=1&signature=UcJdgd4vgt*3AR ...
- centos上安装elasticsearch 5.5.1 遇到的各种坑
mac玩得好好滴,一次性启动成功,结果在centos上安装时,遇坑无数,记录一下: 一.只能localhost访问的问题修改 conf\elasticsearch.yml network.host: ...
- Web登陆实例-—同步username
之前登陆学校的教务系统或者考试系统,进入界面都会有"欢迎***登陆本系统".当时就认为挺高级.如今轮 到自己做这个样例.突然感觉是so easy. 仅仅需简单几步,就可能够搞定. ...
- Delphi 文件遍历
unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- The Win32 Rundll and Rundll32 Interface Related Topics
The Win32 Rundll and Rundll32 Interface Related Topics Microsoft Knowledge Base Article Q164787 Appl ...