130. Surrounded Regions (Graph; DFS)
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
思路:从四条边上的'0'入手,与其邻接的'O'都不被改,记为'D'。逆向思维!从不被改的'O'入手,而非寻找被改得'O'
class Solution {
public:
void dfs(int x, int y){
if(x< || x>=m || y< || y>=n || board[x][y]!='O') return;
board[x][y]='D'; //图的四个方向遍历
dfs(x-,y);
dfs(x+,y);
dfs(x,y-);
dfs(x,y+);
} void solve(vector<vector<char>> &board){
if (board.empty()) return;
this->board = board;
m=board.size();
n=board[].size();
if(n<= || m<=) return; for(int j=;j<n;j++){
dfs(,j);
dfs(m-,j);
} for(int i=;i<m;i++){
dfs(i,);
dfs(i,n-);
} for(int i=;i<m;i++)
for(int j=;j<n;j++){
if(this->board[i][j]=='O') this->board[i][j]='X';
else if(this->board[i][j]=='D') this->board[i][j]='O';
}
board = this->board;
}
private:
int m,n;
vector<vector<char>> board;
};
130. Surrounded Regions (Graph; DFS)的更多相关文章
- 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_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 【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 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 ...
- 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 ...
随机推荐
- L173
Technical problems temporarily blocked some US and European users having access to their accounts an ...
- SELU︱在keras、tensorflow中使用SELU激活函数
arXiv 上公开的一篇 NIPS 投稿论文<Self-Normalizing Neural Networks>引起了圈内极大的关注,它提出了缩放指数型线性单元(SELU)而引进了自归一化 ...
- ps/sql developer 登录远程服务器
Ref PLSQL Developer远程登录的方法
- 阿里云windows时间同步服务地址
偶然发现的, 记录一下 ntp1.aliyun.com
- SM2的非对称加解密java工具类
maven依赖 <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov- ...
- RabbitMQ引入
引入MQ话题 可能很多人有疑惑:MQ到底是什么?哪些场景下要使用MQ? 前段时间安装了RabbitMQ,现在就记录下自己的学习心得吧.首先看段程序: class Program { static vo ...
- CH1808 Milking Grid
题意 POJ2185 数据加强版 描述 Every morning when they are milked, the Farmer John's cows form a rectangular gr ...
- sysfs文件系统学习--sysfs
一.sysfs简介1.sysfs就是利用VFS的接口去读写kobject的层次结构,建立起来的文件系统.其更新与删除是那些xxx_register()/unregister()做的事 情.从sysfs ...
- windows7下安装python环境和django
1.安装python 1.1.首先访问http://www.python.org/download/去下载最新的python版本. 根据计算机位数选择对应的版本比如我的机器是64位的,我就下载这个安装 ...
- System.IO.Path类
System.IO.Path为路径的操作封装了很多很有的东西,利用该类提供的方法能够快速处理路径操作的问题.下面详细了解一下. 1.属性 属性太复杂了,反映什么系统平台的信息,看不懂,等以后看得懂了再 ...