# 130. Surrounded Regions

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

A region is captured by flipping all 'O's into 'X's in that surrounded region.

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

Explanation:

Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.

# Solution

Approach 1: First identify all regions starting from the border and mark them with 'B'. Then fill the target regions with 'X' and overwrite the 'B'-regions back to 'O'.

# Code (Python)

Approach 1:

# Code (C++)

Approach 1:

class Solution {
private:
    /*
    // DFS.
    void exploreBorder(vector<vector<char>>& board, int row, int col) {
        if (board[row][col] != 'O')
            return;
        board[row][col] = 'B';
        if (row > 0)
            exploreBorder(board, row - 1, col);
        if (row < board.size() - 1)
            exploreBorder(board, row + 1, col);
        if (col > 0)
            exploreBorder(board, row, col - 1);
        if (col < board[0].size() - 1)
            exploreBorder(board, row, col + 1);
    }
    */
    // BFS.
    void exploreBorder(vector<vector<char>>& board, int row, int col) {
        if (board[row][col] != 'O')
            return;
        // Overwrite its content at earliest to avoid duplicated queue insert.
        board[row][col] = 'B';
        queue<pair<int,int>> q;
        q.push({row,col});
        while (!q.empty())
        {
            pair<int,int> point = q.front();
            q.pop();
            int i = point.first;
            int j = point.second;
            if (i > 0 && board[i-1][j] == 'O')
            {
                // Overwrite its content at earliest to avoid duplicated queue insert.
                board[i-1][j] = 'B';
                q.push({i-1,j});
            }
            if (i < board.size() - 1 && board[i+1][j] == 'O')
            {
                board[i+1][j] = 'B';
                q.push({i+1,j});
            }
            if (j > 0 && board[i][j-1] == 'O')
            {
                board[i][j-1] = 'B';
                q.push({i,j-1});
            }
            if (j < board[0].size() - 1 && board[i][j+1] == 'O')
            {
                board[i][j+1] = 'B';
                q.push({i,j+1});
            }
        }
    }
public:
    void solve(vector<vector<char>>& board) {
        int rowSize = board.size();
        if (rowSize == 0)
            return;
        int colSize = board[0].size();
        if (colSize == 0)
            return;
        // Explore the border and fill the region with 'B'.
        for (int i = 0; i < colSize; ++i)
        {
            exploreBorder(board, 0, i);
            exploreBorder(board, rowSize - 1, i);
        }
        for (int i = 0; i < rowSize; ++i)
        {
            exploreBorder(board, i, 0);
            exploreBorder(board, i, colSize - 1);
        }
        // Fill the region with 'O'.
        for (int i = 0; i < rowSize; ++i)
        {
            for (int j = 0; j < colSize; ++j)
            {
                if (board[i][j] == 'O')
                    board[i][j] = 'X';
                else if (board[i][j] == 'B')
                    board[i][j] = 'O';
            }
        }
    }
};