<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p>

<ul>
	<li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally or vertically.</li>
	<li><strong>Region</strong>: To form a region <strong>connect every</strong> <code>&#39;O&#39;</code> cell.</li>
	<li><strong>Surround</strong>: The region is surrounded with <code>&#39;X&#39;</code> cells if you can <strong>connect the region </strong>with <code>&#39;X&#39;</code> cells and none of the region cells are on the edge of the <code>board</code>.</li>
</ul>

<p>A <strong>surrounded region is captured</strong> by replacing all <code>&#39;O&#39;</code>s with <code>&#39;X&#39;</code>s in the input matrix <code>board</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]</span></p>

<p><strong>Explanation:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg" style="width: 367px; height: 158px;" />
<p>In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[&quot;X&quot;]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;X&quot;]]</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n == board[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>board[i][j]</code> is <code>&#39;X&#39;</code> or <code>&#39;O&#39;</code>.</li>
</ul>

class Solution {
public:
    void makeX(int i, int j, vector<vector<char>>& mat, vector<vector<int>>& visited){
        mat[i][j] = 'X';
        visited[i][j] = 1;
        if(!visited[i+1][j] && mat[i+1][j] == 'O') makeX(i+1, j, mat, visited);
        if(!visited[i][j+1] && mat[i][j+1] == 'O') makeX(i, j+1, mat, visited);
        if(!visited[i-1][j] && mat[i-1][j] == 'O') makeX(i-1, j, mat, visited);
        if(!visited[i][j-1] && mat[i][j-1] == 'O') makeX(i, j-1, mat, visited);
    }

    void visit(int i, int j, const vector<vector<char>>& mat, vector<vector<int>>& visited){
        visited[i][j] = 1;
        if(i+1<mat.size() && mat[i+1][j] == 'O' && !visited[i+1][j]) visit(i+1, j, mat, visited);
        if(j+1<mat[0].size() && mat[i][j+1] == 'O' && !visited[i][j+1]) visit(i, j+1, mat, visited);
        if(i-1>=0 && mat[i-1][j] == 'O' && !visited[i-1][j]) visit(i-1, j, mat, visited);
        if(j-1>=0 && mat[i][j-1] == 'O' && !visited[i][j-1]) visit(i, j-1, mat, visited);
    }

    bool surrounded(int r, int c, const vector<vector<char>>& mat, vector<vector<int>>& ancestor){
        if(r==0 || c==0 || r==mat.size()-1 || c==mat[0].size()-1) return false;
        ancestor[r][c] = 1;

        if(ancestor[r+1][c] == 0){
            if(mat[r+1][c] == 'O' && !surrounded(r+1, c, mat, ancestor)) return false;
        }
        if(ancestor[r-1][c] == 0){
            if(mat[r-1][c] == 'O' && !surrounded(r-1, c, mat, ancestor)) return false;
        }
        if(ancestor[r][c+1] == 0){
            if(mat[r][c+1] == 'O' && !surrounded(r, c+1, mat, ancestor)) return false;
        }
        if(ancestor[r][c-1] == 0){
            if(mat[r][c-1] == 'O' && !surrounded(r, c-1, mat, ancestor)) return false;
        }

        return true;
    }

    void solve(vector<vector<char>>& board) {
        vector<vector<int>> visited(board.size(), vector<int>(board[0].size(), 0));

        for(int i=0; i<board.size(); i++){
            for(int j=0; j<board[0].size(); j++){
                if(board[i][j] == 'O' && !visited[i][j]){
                    vector<vector<int>> ancestor(board.size(), vector<int>(board[0].size(), 0));
                    if(surrounded(i, j, board, ancestor)) makeX(i, j, board, visited);
                    else visit(i, j, board, visited);
                }
            }
        }
    }
};
24008