Given a 2D board containing 'X' and 'O' (the letter 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 keys: 1. BFS from the most outer layer.
 class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
""" if not board:
return m = len(board)
n = len(board[0]) queue = [] for i in range(m):
for j in range(n):
if i == 0 or j == 0 or i == m -1 or j == n-1:
if board[i][j] == 'O':
queue.append([i,j]) while queue:
[p,q] = queue.pop(0)
board[p][q] ='V'
if self.isValid(p+1, q, m, n) and board[p+1][q] == 'O':
queue.append([p+1, q])
if self.isValid(p-1, q, m, n) and board[p-1][q] == 'O':
queue.append([p-1, q])
if self.isValid(p, q+1, m, n) and board[p][q+1] == 'O':
queue.append([p, q+1])
if self.isValid(p, q-1, m, n) and board[p][q-1] == 'O':
queue.append([p, q-1]) for i in range(m):
for j in range(n):
if board[i][j] == 'V':
board[i][j] ='O'
elif board[i][j] == 'O':
board[i][j] ='X' def isValid(self, i,j, m, n):
return i>0 and i<m and j>0 and j<n
												

Leetcode 130. Surrounded Regions的更多相关文章

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

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

  2. Java for LeetCode 130 Surrounded Regions

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

  3. leetcode 130 Surrounded Regions(BFS)

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

  4. 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 ...

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

  6. 130. Surrounded Regions(M)

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

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

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

  8. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

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

  9. 【leetcode】Surrounded Regions

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

随机推荐

  1. 在SecureCRT中使用rz和sz传输文件

    首先检查Centos中有没有安装 lrzsz sudo yum install lrzsz 使用yum install的时候碰到一个问题, 不知道是否和虚拟机环境有关 Existing lock /v ...

  2. Mysql导出函数、存储过程

    下面是导出存储过程的代码 1 # mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名 其中,-d 表示--no-create-db, -n表示--no-d ...

  3. 表单 - Form - EasyUI提供的表单异步提交

    方案一 被提交的表单 <form id="loginForm" method="post"> <table align="cente ...

  4. 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试

    什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...

  5. Linux system函数详解

    system 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型 int system(cons ...

  6. Spring Security笔记:HTTP Basic 认证

    在第一节 Spring Security笔记:Hello World 的基础上,只要把Spring-Security.xml里改一个位置 <http auto-config="true ...

  7. TinyFrame升级之七:重构Repository和Unit Of Work

    首先,重构的想法来源于以下文章:Correct use of Repository and Unit Of Work patterns in ASP.NET MVC,因为我发现在我的框架中,对Unit ...

  8. Vue系列:通过vue-router如何传递参数

    使用vue-router 来实现webapp的页面跳转,有时候需要传递参数,做法如下: 参考文献:http://router.vuejs.org/en/named.html  主要有以下几个步骤: ( ...

  9. CUDA1.1-函数类型限定符与变量类型限定符

    这部分来自于<CUDA_C_Programming_Guide.pdf>,看完<GPU高性能变成CUDA实战>的第四章,觉得这本书还是很好的,是一种循序渐进式的书,值得看,而不 ...

  10. Windows Azure 云服务角色架构

    当我们使用VS发布一个Cloud Service或者在Portal上上传发布包后,就能启动和运行一个云服务,可以保护WebRole,WorkerRole的一个或者多个实例. Windows Azure ...