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

  solution: 思想是mark 出所有外围的O,里面的不管。

  1. First scan the four edges of the board, if you meet an 'O', call a recursive mark function to mark that region to something else (for example, '+');
  2. scan all the board, if you meet an 'O', flip it to 'X';
  3. scan again the board, if you meet an '+', flip it to 'O';
    class Solution {
    public:
    void BFS(vector<vector<char>> &board, int i , int j){ if(board[i][j] == 'X' || board[i][j] == '#') return;
    board[i][j] = '#'; if( i - >= ) BFS(board, i-, j );
    if( i + < rows ) BFS(board, i+, j );
    if( j - >= ) BFS(board, i, j- );
    if( j + < columns ) BFS(board, i, j+ );
    }
    void solve(vector<vector<char>> &board) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    rows = board.size();
    if( rows == ) return ;
    columns = board[].size();
    if(columns == ) return ; for( int i = ; i < columns - ; i++)
    {
    BFS(board, ,i);//up
    BFS(board, rows - , i );//down
    }
    for( int i = ; i< rows; i++)
    {
    BFS(board, i, ); //left
    BFS(board, i, columns - ); //right
    }
    for(int i = ; i< rows; i++)
    for(int j = ; j < columns ; j++)
    {
    if(board[i][j] == '#')
    board[i][j] = 'O';
    else if (board[i][j] == 'O')
    board[i][j] = 'X';
    }
    }
    private :
    int rows;
    int columns;
    };

LeetCode_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

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

  4. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

  5. [REP]AWS Regions and Availability Zones: the simplest explanation you will ever find around

    When it comes to Amazon Web Services, there are two concepts that are extremely important and spanni ...

  6. Leetcode 130. Surrounded Regions

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

  7. 【leetcode】Surrounded Regions

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

  8. 【leetcode】Surrounded Regions(middle)☆

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

  9. ural 1147. Shaping Regions

    1147. Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) o ...

随机推荐

  1. LeetCode_Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. KEIL的混合编程操作

    http://hi.baidu.com/txz01/item/21ad9d75913a7b28d7a89c12 这一篇来讲讲混合编程的问题,在网上找了一下,讲混合编程的文件章也有不少,但进行实例操作讲 ...

  3. Sessions, Window Stations and Desktops(GetDesktopWindow函数得到的桌面句柄, 是Csrss.exe创建的一个窗口)

    由于不是搞安全的,对这块的东西一直不是很清楚,今天特意整理下. 总的来说,他们的关系如下: 我们知道Windows是支持多用户的,Session可以理解为每个用户登录,比如当前登录和远程登陆.Wind ...

  4. logstash tag使用说明

    zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat stdin04.conf input { stdin { } } filter { # d ...

  5. scp 对拷文件夹 和 文件夹下的所有文件 对拷文件并重命名

    对拷文件夹 (包括文件夹本身) scp -r   /home/wwwroot/www/charts/util root@192.168.1.65:/home/wwwroot/limesurvey_ba ...

  6. Go--包引用介绍

    最近在学习Go编程,本文简单的叙述如何在Go编程中使用包(包管理). 和其他大多数语言一样,Go也存在包,并使用package关键字定义一个包.首先介绍在程序中如何引入包,引入包有以下几种方式: 1. ...

  7. PDO如何选择其他数据库的驱动

    $dsn = "mysql(用的mysql的驱动):dbname=testdb(连接数据库名称);host=127.0.0.1(IP地址,本地是:localhost)"

  8. Annotation(一)——注解开发介绍

    <p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...

  9. JAVA获取oracle中sequences的最后一个值

    项目中,用到一个序列作单号,框架用的是ssh,在dao层去拿的时候,运行时报错为dual is not mapped,[select *.nextval nextvalue from dual] 后来 ...

  10. java Color

    通过16进制颜色值获取颜色方法:Color.decode("#E0EEEE"); --后继续补充