原题地址: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的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  3. LeetCode: Surrounded Regions 解题报告

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  4. Leetcode: Surrounded regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  5. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

  6. LeetCode: Surrounded Regions [130]

    [题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...

  7. [LeetCode] Surrounded Regions 广度搜索

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  8. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  9. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. vc++ 在程序中运行另一个程序的方法

    在vc++ 程序中运行另一个程序的方法有三个: WinExec(),ShellExcute()和CreateProcess() 三个SDK函数: WinExec,ShellExecute ,Creat ...

  2. span 居中

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha ====== 把span 升级为块级元素,div 的宽度.然后使用定位,添加 text- ...

  3. HDU.5985.Lucky Coins(概率DP)

    题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...

  4. HDU 4821 String(2013长春现场赛I题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...

  5. AES advanced encryption standard 3

    This optimized <../aesbench/> AES implementation conforms to FIPS-. aes.h #ifndef _AES_H #defi ...

  6. codeforces round #257 div2 C、D

    本来应该认真做这场的.思路都是正确的. C题,是先该横切完或竖切完,无法满足刀数要求.再考虑横切+竖切(竖切+横切), 由于横切+竖切(或竖切+横切)会对分割的东西产生交叉份数.从而最小的部分不会尽可 ...

  7. [Winform]CefSharp ——js调用c#方法

    摘要 有时我们在winform中嵌入浏览器,需要在页面上读取电脑上的一些信息,这个时候就需要用到CefSharp的RegisterJsObject进行注册方法然后供js进行调用了. 一个例子 我们在w ...

  8. Delphi开发环境中应用层网络协议的实现

    已经进入Internet网络时代了,许多新出的软件都拥有网络功能.其实,在这些软件背后所依靠的技术基础就是一系列的Inernet网络协议标准,亦即TCP/IP系列协议. 下面本人简要介绍一下在Delp ...

  9. CAD扩展属性的提取--FME方式

    一.CAD的扩展属性 了解一下CAD的扩展属性方式,CAD的扩展属性包括二类: 基于CAD二次开发的软件产品(例如南方cass),其扩展属性是附属在图形(点.多段线.注记.面)上面的,它是以XReco ...

  10. 怎样正确的使用Cookie的Path详细解析

    原文地址:http://java-zone.org/1052.html cookie 有路径--path,表示哪些路径下的文件有权限读取该 cookie. path 应该以 “/” 结尾,同名 coo ...