题目来源:

  https://leetcode.com/problems/surrounded-regions/


题意分析:

  给定给一个二维的板,这个板只包括‘X’和‘O’。将被‘X’包围的‘O’变成‘X’。比如:

X X X X
X O O X
X X O X
X O X X
得到:
X X X X
X X X X
X X X X
X O X X

题目思路:

  从板的周围出发,从周围的‘O’出发深度搜索,能搜到的‘O’用visit记录他有没有访问过。最后将所有没有visit过的'O'变成‘X’.


代码(python):

 class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
m = len(board)
if m == 0:
return
n = len(board[0])
visit = [[False for i in range(n)] for j in range(m)]
def dfs(i,j):
q = []
q.append([i,j])
while len(q) != 0:
tmp = q[0]
#print(tmp,visit[3][1],board[3][1])
q.pop(0)
#down,up,left,right
if tmp[0] - 1 > 0 and board[tmp[0] - 1][tmp[1]] == 'O' and visit[tmp[0]-1][tmp[1]] == False:
visit[tmp[0] -1][tmp[1]] = True
q.append([tmp[0] - 1,tmp[1]])
if tmp[0] + 1 < m and board[tmp[0] + 1][tmp[1]] == 'O' and visit[tmp[0]+1][tmp[1]] == False:
visit[tmp[0] +1][tmp[1]] = True
q.append([tmp[0] + 1,tmp[1]])
if tmp[1] - 1 > 0 and board[tmp[0]][tmp[1] - 1] == 'O' and visit[tmp[0]][tmp[1]-1] == False:
visit[tmp[0]][tmp[1] - 1] = True
q.append([tmp[0],tmp[1] - 1])
if tmp[1] + 1 < n and board[tmp[0]][tmp[1] + 1] == 'O' and visit[tmp[0]][tmp[1]+1] == False:
visit[tmp[0]][tmp[1]+1] = True
q.append([tmp[0],tmp[1]+1])
for i in range(n):
if visit[0][i] == False and board[0][i] == 'O':
visit[0][i] = True
dfs(0,i)
if visit[m - 1][i] == False and board[m-1][i] == 'O':
visit[m-1][i] = True
dfs(m - 1,i)
for j in range(m):
if visit[j][0] == False and board[j][0] == 'O':
visit[j][0] = True
dfs(j,0)
if visit[j][n - 1] == False and board[j][n - 1] == 'O':
visit[j][n - 1] = True
dfs(j,n - 1)
for i in range(m):
for j in range(n):
if board[i][j] == 'O' and not visit[i][j]:
board[i][j] = 'X'

[LeetCode]题解(python):130-Surrounded Regions的更多相关文章

  1. 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 用了第一种方式, ...

  2. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

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

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

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

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

  5. 【一天一道LeetCode】#130. Surrounded Regions

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. leetcode 130 Surrounded Regions(BFS)

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

  7. Leetcode 130. Surrounded Regions

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

  8. Java for LeetCode 130 Surrounded Regions

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

  9. Leetcode 130 Surrounded Regions DFS

    将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...

  10. 130. Surrounded Regions

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

随机推荐

  1. Android开发10.3:UI组件GridView网格视图

    GridView(网格视图) 概述 GridView用于在界面上按行.列分布的方式来显示多个组件         GridView和ListView有共同的父类 : AbsListView       ...

  2. android可拖动排序GridView实现

    经常使用今日头条.网易新闻的同学们应该都会注意到用于管理多个频道的可拖动排序GridView,下面介绍一下可拖动的DragGridView的实现方法.代码放在GitHub上https://github ...

  3. Linux前台的程序转到后台执行(关闭终端而不杀死命令)

    你是否经常遇到这样的情况,通过SSH或者终端putty连接到一台linux/unix机器,执行一个程序.一个脚本或者一条命令,但现在你需要关闭SSH或者终端,由于该该程序.脚本或者命令正在运行,一旦你 ...

  4. C# 第三方控件 错误 LC-1

    删掉项目下面的Properties\licenses.licx 文件

  5. English - even though和even if用法解析

    even though和even if的用法区别两者均可用于引导让步状语从句,其细微区别是: 1. even if 引导的从句是往往是假设性的,相当于汉语的“即使”“纵然”“就算”“哪怕”. 如:Th ...

  6. SET QUOTED_IDENTIFIER (Transact-SQL)

    使 SQL Server 遵从关于引号分隔标识符和文字字符串的 ISO 规则. 由双引号分隔的标识符可以是 Transact-SQL 保留关键字,也可以包含 Transact-SQL 标识符语法约定通 ...

  7. IIS发布网站后局域网其他用户不能访问问题(转)

    如果本机能正常访问,而局域网其他用户不能访问,那么判断的结果很可能是防火墙问题. 解决方法: 既然问题出在Windows7或Windows Server 2008 R2的防火墙上,那么我们可以有以下两 ...

  8. Log4net 自定义字段到数据库(二)

    这种方法比第一种方法麻烦些 Log4Net.config <?xml version="1.0" encoding="utf-8" ?> <c ...

  9. 原生js动态改变dom高度

    item参数为要改变高度的dom,maxHight参数为dom的最大高度,speed参数为改变高度的速度function addHeight(item,maxHight,speed){ var ite ...

  10. jQuery实现导航监听事件

    导航html如下 <div class="main_nav"> <a class="nav_01 active_01" href=" ...