题目描述

现在有一个仅包含‘X’和‘O’的二维板,请捕获所有的被‘X’包围的区域
捕获一个被包围区域的方法是将被包围区域中的所有‘O’变成‘X’
例如
X X X X↵X O O X↵X X O X↵X O X X

执行完你给出的函数以后,这个二维板应该变成:

X X X X↵X X X X↵X X X X↵X O X X

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
class Solution {
public:
    void solve(vector<vector<char>> &board) {
        if(board.empty())
            return;
        int rows = board.size();
        int cols = board[0].size();
         
        if(rows==0 || cols==0)
            return;
         
        for(int j=0;j<cols;j++)
        {
            DFS(board, 0, j);
            DFS(board, rows-1, j);         }                  for(int i=0;i<rows;i++)         {             DFS(board, i, 0);             DFS(board, i, cols-1);         }                  for(int i=0;i<rows;i++)             for(int j=0;j<cols;j++)                 if(board[i][j] == 'O')                     board[i][j] = 'X';                  for(int i=0;i<rows;i++)             for(int j=0;j<cols;j++)                 if(board[i][j] == '*')                     board[i][j] = 'O';
    }
    void DFS(vector<vector<char> > &board, int r, int c)
    {
        if(board[r][c] == 'O')
        {
            board[r][c] = '*';             int rows = board.size();             int cols = board[0].size();                          if(r > 1)                 DFS(board, r-1, c);             if(r < rows-2)                 DFS(board, r+1, c);             if(c > 1)                 DFS(board, r, c-1);             if(c < cols-2)                 DFS(board, r, c+1);                     }     }
};

leetcode21 surrounded regions的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

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

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

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

  3. 【leetcode】Surrounded Regions

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

  4. [LintCode] Surrounded Regions 包围区域

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

  5. 22. Surrounded Regions

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

  6. Surrounded Regions

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

  7. [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions

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

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

  9. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

随机推荐

  1. Matlab2016b安装流程

    来源:https://jingyan.baidu.com/article/59703552da12ab8fc007402b.html Matlab2016b安装教程 听语音 原创 | 浏览:34338 ...

  2. MySQL 查询字段时,区分大小写

    设置排序规则: 区分大小写的查询: mysql> select * from user; +----+----------+-----------+------+------+ | id | u ...

  3. XML流操作

    /// <summary>         /// 保存XML为指定格式         /// </summary>         /// <param name=& ...

  4. VS2019 配置opencv4.4

    安装VS2019 参考:链接 下载opencv 链接 下载此时的最新版4.4.0 最后"上墙"下,不然很慢! 安装opencv 我的安装位置是:D:\soft\opencv\ins ...

  5. 多测师肖sir_pdf转word方法

    1.百度搜索 my love  pdf      在线转换 2.输入wps  下载软件

  6. 多测师讲解python _函数中参数__高级讲师肖sir

    函数中讲解参数: 形参和实参的认识 函数无参数的调用 函数单个参数的调用 函数多个参数的调用 # #调试函数给默认参数传新值,则函数使用新值 # 注意:当多种参数同时出现在函数中,默认参数要放在最后的 ...

  7. 【Curl】【转】curl用法!

    curl基础用法! www.ruanyifeng.com/blog/2019/09/curl-reference.html

  8. MeteoInfoLab脚本示例:多Y轴图

    数据范围相差比较大的数据序列进行对比的时候多Y轴图就很重要了.MeteoInfoLab中提供了一个twinx函数来根据已有的坐标系(Axes)生成一个新的Axes,这个命令会使得已有的Axes不绘制右 ...

  9. ansible通过yum/dnf模块给受控机安装软件(ansible2.9.5)

    一,使用yum/dnf模块要注意的地方: 使用dnf软件安装/卸载时,需要有root权限, 所以要使用become参数 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnb ...

  10. CentOS 6编译安装RabbitMQ

    编译安装Python 下载python源文件 [root@localhost src]# tar -xzvf python-2.7.11.tar.gz [root@localhost src]# cd ...