原题地址: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. “百度杯”CTF比赛 九月场 YeserCMS

    打开题目 进入后是一个cms,但肯定的是这个cms不叫yesercms 于是我们开始随便翻翻,寻找信息,后台我也看了除了一个登陆界面,就没有其他的提示信息. 最后在文档下载的评论栏里发现,这个cms的 ...

  2. Android-贪吃蛇小游戏-分析与实现-Kotlin语言描述

    Android-贪吃蛇小游戏-分析与实现-Kotlin语言描述 Overview 本章的主要的内容是贪吃蛇小游戏的分析和实现,关于实现的具体代码可以在,文章底部的github的链接中找到. 整个游戏通 ...

  3. 领英Linkedin信息搜集工具InSpy

    领英Linkedin信息搜集工具InSpy   领英Linkedin是一个知名职业社交媒体网站.通过该网站,渗透测试人员可以获取公司内部组成和员工信息.Kali Linux提供一款专用的信息收集工具I ...

  4. 【NOIP复习】最短路总结

    [模板] /*堆优化Dijkstra*/ void dijkstra() { priority_queue<pair<ll,int>,vector<pair<ll,int ...

  5. Jenkins持续集成环境, 如何自定义 maven repositories

    假设自定义的仓库路径为“/opt/repository”,那么在“系统管理-系统设置”中,修改“全局MAVEN_OPTS”的值为如下的内容: -Dmaven.repo.local=/opt/repos ...

  6. CTSC被虐记

    退役前写写破事乐呵乐呵..(雾 Day0 愉快的没有分到另一个宾馆...但是是个单间...而且居然是大床房...难以置信, 试机向BeiYe学习了一发Gedit的外部工具, 试到一般好像都走了..只剩 ...

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心

    D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...

  8. Codeforces Round #368 (Div. 2) A. Brain's Photos 水题

    A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...

  9. 初探C++类模版学习笔记

    类模板 实现:在定义类的时候给它一个或多个參数,这个些參数表示不同的数据类型.                              -->抽象的类. 在调用类模板时, 指定參数, 由编译系 ...

  10. Control reaches end of non-void function 犯过最傻的错误

    之所以会报“Control reaches end of non-void function ”的警告,时因为方法名中缺少返回类型.正确的写法如下: +(void)setMobile:(NSStrin ...