<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 solve(vector<vector<char>>& board) {
        int board_r = board.size();
        int board_c = board[0].size();
        unordered_set<string> visited;
        vector<pair<int, int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        vector<vector<pair<int, int>>> modify;
        for (int r = 0; r < board_r; r++) {
            for (int c = 0; c < board_c; c++) {
                if (board[r][c] == 'O' && visited.find(to_string(r) + "," + to_string(c)) == visited.end()) {
                    bfs(board, r, c, board_r, board_c, visited, dirs, modify);
                }
            }
        }
        for (auto& region : modify) {
            for (auto& pos: region) {
                board[pos.first][pos.second] = 'X';
            }
        }
        return;
    }

    void bfs(vector<vector<char>>& board, int r, int c, int board_r, int board_c, unordered_set<string>& visited,  vector<pair<int, int>>& dirs, vector<vector<pair<int, int>>>& modify) {
        vector<pair<int, int>> region;
        queue<pair<int, int>> q;
        q.push({r, c});
        visited.insert(to_string(r) + "," + to_string(c));
        region.push_back({r, c});
        bool include_region = true;
        while (!q.empty()) {
            int cr = q.front().first;
            int cc = q.front().second;
            if (cr == 0 || cr == board_r-1 || cc == 0 || cc == board_c-1) {
                include_region = false;
            }
            q.pop();
            for (auto& dir : dirs) {
                int nr = cr + dir.first;
                int nc = cc + dir.second;
                if (nr >= 0 && nr < board_r && nc >= 0 && nc < board_c && board[nr][nc] == 'O' && visited.find(to_string(nr) + "," + to_string(nc)) == visited.end()) {
                    q.push({nr, nc});
                    visited.insert(to_string(nr) + "," + to_string(nc));
                    region.push_back({nr, nc});
                }
            }
        }
        if (include_region) {
            modify.push_back(region);
        }
        return;
    }
};
22628