Leetcode 130 Surrounded Regions DFS
将内部的O点变成X
input
X X X X
X O O X
X X O X
X O X X
output
X X X X
X X X X
X X X X
X O X X
DFS的基本框架是
void dfs(int now,int d){
if(终止条件) {
做相应的操作;
return;
}
for(遍历所有now点的相邻点next){
if(!visit[next]) {
访问每个没有访问过的点;
做相应的操作;
dfs(next, d + );
}
}
}
DFS图所有边上的点,将边上的O以及其联通域变为T,然后将内部的O变为X,外部的T变为O,本质是种子填充算法。
此题对于is_in这个函数要注意, 不要用(x < m) && (x >= 0) && (y < n) && (y >= 0) 会堆栈溢出!!原因是会出现边上的点全是O。。。
class Solution {
public:
int m, n;
bool is_in(int x, int y)
{
return (x < m-) && (x >= ) && (y < n-) && (y >= );
}
void dfs(std::vector<std::vector<char>> &board,int x,int y)
{
//if (!(is_in(x, y) && board[x][y] == 'O')) return;
//printf("%d %d\n", x, y);
board[x][y] = 'T';
int dir[][] = { { , }, { -, }, { , }, { , - } };
for (int i = ; i < ; ++i){
int tx = x + dir[i][];
int ty = y + dir[i][];
if (is_in(tx, ty) && board[tx][ty] == 'O')
{
dfs(board, tx, ty);
}
}
}
void change(std::vector<std::vector<char>> &board)
{
for (int i = ; i < m;++i){
for (int j = ; j < n;++j){
if (board[i][j] == 'T') board[i][j] = 'O';
else if (board[i][j] == 'O') board[i][j] = 'X';
else;
}
}
}
void solve(std::vector<std::vector<char>> &board)
{
m = board.size();
if (m == ) return;
n = board[].size();
if (n == ) return;
for (int i = ; i < m;++i){
if (board[i][] == 'O') dfs(board, i, );
if (board[i][n - ] == 'O') dfs(board, i, n-);
}
for (int i = ; i < n; ++i){
if (board[][i] == 'O') dfs(board, , i);
if (board[m-][i] == 'O') dfs(board, m-, i);
}
change(board);
}
};
Leetcode 130 Surrounded Regions DFS的更多相关文章
- [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(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 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_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】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- linux 无法解压过大文件解决
[root@vmbbak yum]# unzip RHEL_5.7\ x86_64\ DVD-1.zip error: Zip file too big (greater than 429495910 ...
- deep learning
今天跑一个模型,程序都没变,就配置文件变了.但是总是很快就显示loss为nan. 检查配置文件还是不行,把其中loss改为0还是不行.最后搁置了一下,再回头对比一下电脑上的和服务器上的,发现一个配置文 ...
- 算法与数据结构实验题 5.2 Missile
1.题目: 2.解题思路: 把每个点对应的两条半径求出,之后对d1进行升序排序,对应d2也改变位置.其中一个圆心的半径r1确定之后,除去第一个圆包围的点,在其余点中找到另外一个圆的最长的半径r2,此时 ...
- 彻底卸载sublime txt
最近彻底重装系统之后,安装sublime txt3, 自己设置了一些,总是觉得不是很对劲,想重新安装. 结果每次安装之后,总是有一些配置文件和卸载之前的是一样的,重复几次总是如此,于是网上搜资料,怎么 ...
- 使用 xlrd 模块实现对excel 的读取、excel转json 、excel 转 mysql insert 语句
#-*- coding:utf-8 -*- # 处理 excel 中的 area 为 Mysql insert 语句 import xlrd, json, codecs, os # data = xl ...
- deepdetect 用c++11写的机器学习caffe和XGBoost API 接口
https://github.com/beniz/deepdetect DeepDetect (http://www.deepdetect.com/) is a machine learning AP ...
- 访问https链接方法
<a id='___szfw_logo___' href='https://credit.szfw.org/CX20160808028375110138.html' target='_blank ...
- Linux下cutecom使用USB转串口线
http://www.cnblogs.com/pang123hui/archive/2011/05/29/2309888.html 在Linux下的串口调试一直使用minicom,虽说Linux的精髓 ...
- mysql 全文搜索的FULLTEXT
FULLTEXT索引 创建FULLTEXT索引语法 创建table的时候创建fullText索引 CREATE TABLE table_name( column1 data_type, column2 ...
- Mac mysql修改密码
在网上看了很多的办法,其实解决办法都对,只是没有说明白: 1,首先启动mysql服务 2,mysqladmin -uroot -p 'newpassword' 此时需要输入登陆密码(数据库的密码,刚安 ...