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++

  1. class Solution {
  2. void dfs(vector<vector<char> > &board,int row,int col){
  3. if (board[row][col]=='O'){
  4. board[row][col]='*';
  5. if (row<board.size()-1) dfs(board,row+1,col);
  6. if (col<board[0].size()-1) dfs(board,row,col+1);
  7. if (row>1) dfs(board,row-1,col);
  8. if (col>1) dfs(board,row,col-1);
  9. }
  10. }
  11. public:
  12. void solve(vector<vector<char>> &board) {
  13. if (board.size()<1 || board[0].size()<1) return;
  14. int row = board.size(),col = board[0].size();
  15. for(int i=0;i<row;++i){
  16. dfs(board,i,0);
  17. dfs(board,i,col-1);
  18. }
  19. for(int i=0;i<col;++i){
  20. dfs(board,0,i);
  21. dfs(board,row-1,i);
  22. }
  23. for(int i=0;i<row;++i){
  24. for(int j=0;j<col;++j){
  25. if (board[i][j]=='O') board[i][j]='X';
  26. if (board[i][j]=='*') board[i][j]='O';
  27. }
  28. }
  29. }
  30. };

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. java.net.NoRouteToHostException: 无法指定被请求的地址

    今天压力测试时, 刚开始出现了很多异常, 都是 java.net.NoRouteToHostException: Cannot assign requested address.  经网上查资料, 是 ...

  2. php各个版本curl上传文件的兼容实现

    // 以POST方式提交数据 public function post_data($url, $param, $is_file = false, $return_array = true) { ... ...

  3. canal源码之BooleanMutex(基于AQS中共享锁实现)

    在看canal源码时发现一个有趣的锁实现--BooleanMutex 这个锁在canal里面多处用到,相当于一个开关,比如系统初始化/授权控制,没权限时阻塞等待,有权限时所有线程都可以快速通过 先看它 ...

  4. Django边学边记—新手Django建项目各流程细节

    一.准备虚拟环境 1)安装 virtualenv pip install virtualenv 2)virtualenvwrapper pip install virtualenvwrapper-wi ...

  5. jmeter5.2版本 配置元件之逻辑控制器详解

    1.简单控制器(Simple Controller) 作用:将多个请求放置在一起,但是没有逻辑上的操作,进行一个简单的分组,一般是由于分组后的请求需要进行统一的某个操作或者存在共同的因素.在简单控制器 ...

  6. Jenkins持续交付实战演练

    jenkins web hook机制 运行jenkins任务触发方式: 主动运行 定时构建 就算代码库没有更新,也会构建. 通过代码库主动触发Jenkins的构建任务 jenkins向外暴露一个触发器 ...

  7. CSS 奇技淫巧 | 妙用混合模式实现文字镂空波浪效果

    本文将介绍一个小技巧,通过混合模式 mix-blend-mode 巧妙的实现文字的镂空波浪效果. 起因 一日,一群友私聊问我.如何使用 CSS 实现下述效果,一个文字的波浪效果: 我当时想都没想,就回 ...

  8. P7737-[NOI2021]庆典【tarjan,虚树】

    正题 题目链接:https://www.luogu.com.cn/problem/P7737 题目大意 给出一张无向图满足若\(x\Rightarrow z,y\Rightarrow z\)那么有\( ...

  9. P4091-[HEOI2016/TJOI2016]求和【斯特林数,NTT】

    正题 题目链接:https://www.luogu.com.cn/problem/P4091 题目大意 给出\(n\),求 \[\sum_{i=0}^n\sum_{j=0}^i\begin{Bmatr ...

  10. 1-SQL Server2019安装

    sql server2019安装 首先去官网下载(下载express版本): 打开安装程序 选择自定义 更改一下安装目录,点击安装 等待安装 等安装完成后,出现如下页面 选择SQL Server独立安 ...