将内部的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的更多相关文章

  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. leetcode 130 Surrounded Regions(BFS)

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

  3. Leetcode 130. Surrounded Regions

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

  4. Java for LeetCode 130 Surrounded Regions

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

  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_Medium tag: DFS/BFS

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

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

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

  9. 【leetcode】Surrounded Regions

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

随机推荐

  1. ALV界面将可编辑字段值保存到内表中

    具体代码如下: data: lr_grid type ref to cl_gui_alv_grid.      data: l_valid type c.      read table gt_exc ...

  2. 杀死你网站SEO的5个技术

    胡亮亮先生(网迈SEO总监)在其微信公众帐号里发布了文章<杀死你网站SEO的5个技术>,发出来给大家分享一下: 应百度站长平台邀请,抽空把这篇文章做一些细节上的补充 ,欢迎大家关注并讨论. ...

  3. 【网站运营】网站被K的原因大总结

    对于广大的站长来说网站被K或者是被降权是经常有的事情,不过我基本上还没有看见过Google的K站情况,也就是给网站降个权什么的处罚.如果你是用了很严重的作弊手段的话,那指定会是被Google给K掉的. ...

  4. VS2015 建立C++ dll库文件

    最近在写一个图片处理,正好用到C++封装DLL给C#调用,一下是总结:   建立一个C++的Win32DLL,这里要注意选择"Export symbols"导出符号.点击完成. 如 ...

  5. 购物车界面,不同section,点击增减物品,确定取消选中的逻辑判断

    1.首先在自定义的cell中,创建两个代理方法 @protocol shopCartDelegate <NSObject> -(void)shopCartDelegate:(ShopCar ...

  6. 【区间dp】codevs1966 乘法游戏

    f(i,j)=min{f(i,k)+f(k,j)+a[i]*a[k]*a[j]}(1<=i<=j<=n,i<k<j) #include<cstdio> #in ...

  7. 斯坦福第十六课:推荐系统(Recommender Systems)

    16.1  问题形式化 16.2  基于内容的推荐系统 16.3  协同过滤 16.4  协同过滤算法 16.5  矢量化:低秩矩阵分解 16.6  推行工作上的细节:均值归一化 16.1  问题形式 ...

  8. MySQL Connector/J 6.x jdbc.properties 配置, mysql-connector-java-6.0.4.jar 异常

    今天学习SSM框架整合,完成Spring和mybatis这两大框架的整合做测试时候出来很多问题,主要来自于配置文件. 我这里重点说一下Mysql数据驱动配置. 配置pom.xml时候去网站 MySQL ...

  9. IE6兼容透明JS

    <!--兼容png格式图片--> <!--[]> <script type="text/javascript" src="Js/DD_bel ...

  10. UIScrollView详解

    1.UIScrollView常用属性 contentSize属性--该属性表示滚动的内容的范围大小,是CGPoint类型的. 说明: 默认超出UIScrollView的可视区域的内容是不显示的.相当于 ...