[LeetCode]题解(python):130-Surrounded Regions
题目来源:
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的更多相关文章
- 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 ...
- [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 ...
- 【一天一道LeetCode】#130. Surrounded Regions
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- leetcode 130 Surrounded Regions(BFS)
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 reg ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 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 ...
- 130. Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
随机推荐
- ZooKeeper源码阅读(二):客户端
源代码: http://svn.apache.org/repos/asf/zookeeper/trunk/ 导入eclipse: 在包含build.xml目录下执行ant eclipse将产生.cla ...
- SPOJ QTREE4 lct
题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...
- sql server 2005 表master..spt_values
IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #tGOcreate table #t(id int identity,Dt varchar(10) ...
- windows平台下杀死指定端口的进程(转载)
在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\>netstat -ano 协议 本地地址 外部地址 ...
- 重启sql server服务两种方式
1.第一种重启SQl server的方法 点击左侧的数据库重启 如图 右击有个重启操作 2.第二个重启sql server方法
- office2010安装出错,windows installer服务不能更新一个或多个受保护的windows文件
转自:http://www.08lr.cn/article/1985.html office2010安装过程中出现如下图错误:windows installer 服务不能更新一个或多个受保护的wind ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- poj2175
鸣谢: http://www.cppblog.com/y346491470/articles/152317.html [题意]:一个城市有n座建筑物,每个建筑物里面有一些人,为了在战争爆发时这些人都可 ...
- win7中注册tomcat服务
非安装版tomcat下载后,在bin文件夹会有一个startup.bat文件,运行该文件即可启动tomcat了.不过在服务器配置tomcat的话,就通常需要注册为服务. 在/bin文件下还有tomca ...
- NAND闪存颗粒结构及工作原理
NAND闪存是一种电压元件,靠其内存电压来存储数据,现在我们就来谈谈它的结构及工作原理. 闪存的内部存储结构是金属-氧化层-半导体-场效晶体管(MOSFET),里面有一个浮置栅极(Floating G ...