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. 【数位DP】 HDU 4734 F(x)

    原题直通车:HDU 4734 F(x) 题意:F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1, 求0.....B中F[x]<=F[A ...

  2. TranslateAnimation详解

    TranslateAnimation详解 Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, Tr ...

  3. Linux企业级项目实践之网络爬虫(15)——区分文本文件和二进制文件

    HTTP协议支持文本和二进制文件传输.最常见的html格式的页面即文本,图片.音乐等为二进制文件.我们要对这两类文件加以区分并分别处理. static char * BIN_SUFFIXES = &q ...

  4. 【转】Linux內核驅動之GPIO子系統(一)GPIO的使用 _蝸牛

    原文网址:http://tc.chinawin.net/it/os/article-2512b.html 一 概述 Linux內核中gpio是最簡單,最常用的資源(和interrupt ,dma,ti ...

  5. bzoj2929 [Poi1999]洞穴攀行

    Description 一队洞穴学者在Byte Mountain的Grate Cave里组织了一次训练.训练中,每一位洞穴学者要从最高的一个室到达最底下的一个室.他们只能向下走.一条路上每一个连续的室 ...

  6. map循环遍历删除

    typedef map<string,int> MapFileList; int main() { MapFileList m_SingleList; m_SingleList.inser ...

  7. 单片微机原理P4:80C51串口与串行总线拓展

    0. 串口通讯 0. 串口通讯的数据传输方式:单工(单向传输数据),半双工(非同时双向传输),全双工(同时,双向传输) 1. 根据通信方式的不同又分为同步通讯和异步通讯. 同步通讯:所有设备都使用同一 ...

  8. swift 自定义导航栏颜色

    func setNavigationApperance(){ //自定义导航栏颜色 [self.navigationController?.navigationBar.barTintColor = U ...

  9. iOS之即时通讯相关理解

    Socket: 1>Socket又称"套接字" 2>网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 3>应用程序通常通 ...

  10. NetAnalyzer笔记 之 五 一些抓包技巧分享(不定期更新)

    [创建时间:2016-03-12 10:00:00] [更新时间:2016-05-21 10:00:00] NetAnalyzer下载地址 前一段时间应为工作关系,NetAnalyzer笔记系列已经很 ...