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_Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. Qt编程之实现属性窗口编辑器

    类似于这种: 就是右下角这个框,有属性名字和对应的value编辑. 这个Widget是作为一个QDockWidget被添加到QMainWindow中的.QMainWindow必须要有centralWi ...

  3. rpm -qs查看包信息

    rpm -qs mysql-connector-c-devel Query options (with -q or --query):  -c, --configfiles               ...

  4. QA笑话----杂思

    QA工程师走进酒吧,要了一杯啤酒,要了0杯啤酒,要了999999999杯啤酒,要了一只蜥蜴,要了-1杯啤酒,要了一个sfdeljknesv,酒保从容应对,QA工程师 很满意.接下来,一名顾客来到了同一 ...

  5. python3-day3(内置函数)

    1.内置函数 1>print(bytearray('王',encoding='utf8')) 2>print(bytes('王',encoding='utf8')) 3>bool(' ...

  6. [Cycle.js] Read effects from the DOM: click events

    So far we only had effects that write something to the external world, we are not yet reading anythi ...

  7. OpenWrt sscanf问题之于MT7620N与AR9341

    在MT7620N平台做好了wifidog的相关调试工作,除了eth驱动.wireless性能问题,其余的都能够基本正常. 依据实际须要要对已完毕的工作在AR9341平台上实现. 事实上也简单.基本功能 ...

  8. android避免service被杀

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建@Override    public int onStartComma ...

  9. Enabling Active Directory Authentication for VMWare Server running on Linux《转载》

    Enabling Active Directory Authentication for VMWare Server running on Linux Version 0.2 - Adam Breid ...

  10. 解决System.Data.SQLite兼容32位和64位问题

    将当前说明文档的目录下的x64.x86目录和System.Data.SQLite.dll文件复制到您的应用程序根目录中(注意更新引用,引用System.Data.SQLite.dll即可,两目录中的不 ...