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

C++

class Solution {
void dfs(vector<vector<char> > &board,int row,int col){
if (board[row][col]=='O'){
board[row][col]='*';
if (row<board.size()-1) dfs(board,row+1,col);
if (col<board[0].size()-1) dfs(board,row,col+1);
if (row>1) dfs(board,row-1,col);
if (col>1) dfs(board,row,col-1);
}
}
public:
void solve(vector<vector<char>> &board) {
if (board.size()<1 || board[0].size()<1) return;
int row = board.size(),col = board[0].size(); for(int i=0;i<row;++i){
dfs(board,i,0);
dfs(board,i,col-1);
}
for(int i=0;i<col;++i){
dfs(board,0,i);
dfs(board,row-1,i);
} for(int i=0;i<row;++i){
for(int j=0;j<col;++j){
if (board[i][j]=='O') board[i][j]='X';
if (board[i][j]=='*') board[i][j]='O';
}
}
}
};

surrounded-regions leetcode C++的更多相关文章

  1. Surrounded Regions——LeetCode

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

  2. Surrounded Regions leetcode java

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

  3. Surrounded Regions - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 https://leetcode.com/problems/surrounded-regions/ 注意点 边缘不算包围'O' 解法 解法一:dfs.找处 ...

  4. [LeetCode] Surrounded Regions 包围区域

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

  5. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

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

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

  7. Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)

    Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...

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

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

  9. 【leetcode】Surrounded Regions

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

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

随机推荐

  1. 机器学习——K-Means算法

    1 基础知识 相似度或距离 假设有 $m$ 个样本,每个样本由 $n$ 个属性的特征向量组成,样本合集 可以用矩阵 $X$ 表示 $X=[x_{ij}]_{mn}=\begin{bmatrix}x_{ ...

  2. 【C++基础教程】第二课

    一,上次的课后练习答案 1,输出1+2=3 2,输出2 2.25 2.25 2.25 3,第一空iostream或bits/stdc++.h 第二空main(),main(void)或main(int ...

  3. python 打字小游戏

    最近随便用python的pygame编了这个打字小游戏 只要有字母调到窗口底部就结束 上代码: import pygame.freetype import sys import random pyga ...

  4. mapper-spring-boot-starter 主要作用是

    今天是第一次接触到 这个场景启动器内心中真是一片的茫然,学习了这么长时间我居然还不知道有这个的存在今天好好查一查资料 参考资料(https://blog.csdn.net/crq1205/articl ...

  5. P4756-Added Sequence【斜率优化】

    正题 题目链接:https://www.luogu.com.cn/problem/P4756 题目大意 给出序列\(a\),设\(f(l,r)=|\sum_{i=l}^ra_i|\). \(m\)次询 ...

  6. POJ3734-Blocks【EGF】

    正题 题目链接:http://poj.org/problem?id=3734 题目大意 用思种颜色给\(n\)个格子染色,要求前两种颜色出现偶数次,求方案. \(1\leq T\leq 100,1\l ...

  7. SpringMVC的数据输出

    使用 @Controller public class OutputController { @RequestMapping("/handle01") public String ...

  8. State Space Model Content

    State Space Model 状态空间模型及其卡尔曼滤波技术 混合正态分布下的状态空间模型及其滤波

  9. Xamarin Android使用自签名证书

    背景 项目中后台web服务部署成https服务时,需要使用SSL证书,如果我们不使用公共的CA时,怎么办? 不仅如此,因为是小项目,App应用主要是小范围使用,此时只有IP地址,根本没有域名,怎么办? ...

  10. (目录)Fortran学习笔记:开坑!!!

    前言:因为某些原因,需要使用Fortran编写程序,记录下Fortran语法学习过程中的部分笔记.在此开坑记录,立下Flag,"希望年末能够更新完" Fortran 学习笔记 陈橙 ...