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 ...
随机推荐
- Android Studio 编译报错:Process 'command 'D:\SDK\AS\sdk\build-tools\23.0.0\aapt.exe'' finished with non-zero exit value 1
AGPBI: {"kind":"error","text":"No resource identifier found for a ...
- java并发编程之一--Semaphore的使用
1.介绍 Semaphore 中文的含义 信号,信号系统,此类的只要作用就是限制线程的并发的数量. Semaphore内部主要通过AQS(AbstractQueuedSynchronizer)实现线程 ...
- 利用U盘大白菜软件来重装win7系统
个人装win7系统用了两个U盘,一个做启动盘(FAT32格式),另外一个当做系统盘(NTFS格式). 首先在电脑里面下载一个大白菜软件,并且安装好,打开软件,插上U盘,检测到了该U盘即可一键制作启动盘 ...
- Yii在window下的安装方法
首先,在http://www.yiichina.com/上下载yii 然后,配置系统环境变量,在win8下,按win+x,找到系统->高级系统设置->环境变量->path 把php的 ...
- 《DSP using MATLAB》Problem 2.16
先由脉冲响应序列h(n)得到差分方程系数,过程如下: 代码: %% ------------------------------------------------------------------ ...
- nexus 使用Raw Repositories 进行maven site 发布
实际项目中我们可能需要进行maven 项目的site 文档发布,一般的处理是生成之后,然后在进行发布到一个静态 服务器进行页面访问,nexus3 提供了一个Raw Repositories 很方便可以 ...
- pthread信号
信号是典型的异步事件.内核在某个信号出现时有三种处理方式: 忽略信号,除了SIGKILL和SIGSTOP信号不能忽略外,其他大部分信号都可以被忽略: 捕捉信号,也就是在信号发生时调用一个用户函数,注意 ...
- Http消息头中常用的请求头和响应头
作为Web开发对常用http的请求头和响应头熟悉了解一下还是很有必要的.比如请求头中Content-type指定了请求的内容,若类型是application/x-www-form-urlencoded ...
- [LeetCode系列] 跳跃问题II
给定一系列非负整数, 每个值代表从此下标可以向前跳跃的最远距离, 试求出跳跃到数组尾端需要的最少步骤. 如给定 [2,3,1,1,4], 返回2. (从下标0跳到1, 从1跳到下标4). 算法描述: ...
- 【转】vim环境设置和自动对齐
原文网址:http://blog.chinaunix.net/uid-23525659-id-4340245.html 注:如果是用vim编写代码,建议开启vim的文件类型自动检测功能,这样编写代码换 ...