# 被围绕的区域

给你一个 <code>m x n</code> 的矩阵 <code>board</code> ，由若干字符 <code>'X'</code> 和 <code>'O'</code> ，找到所有被 <code>'X'</code> 围绕的区域，并将这些区域里所有的 <code>'O'</code> 用 <code>'X'</code> 填充。
<div class="original__bRMd">
<div>
<p> </p>

<p><strong>示例 1：</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg" style="width: 550px; height: 237px;" />
<pre>
<strong>输入：</strong>board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
<strong>输出：</strong>[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
<strong>解释：</strong>被围绕的区间不会存在于边界上，换句话说，任何边界上的 <code>'O'</code> 都不会被填充为 <code>'X'</code>。 任何不在边界上，或不与边界上的 <code>'O'</code> 相连的 <code>'O'</code> 最终都会被填充为 <code>'X'</code>。如果两个元素在水平或垂直方向相邻，则称它们是“相连”的。
</pre>

<p><strong>示例 2：</strong></p>

<pre>
<strong>输入：</strong>board = [["X"]]
<strong>输出：</strong>[["X"]]
</pre>

<p> </p>

<p><strong>提示：</strong></p>

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n == board[i].length</code></li>
	<li><code>1 <= m, n <= 200</code></li>
	<li><code>board[i][j]</code> 为 <code>'X'</code> 或 <code>'O'</code></li>
</ul>
</div>
</div>


## template

```cpp
#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    int m, n;
    void solve(vector<vector<char>> &board)
    {
        m = board.size();
        if (!m)
            return;
        n = board[0].size();
        if (!n)
            return;
        for (int i = 0; i < m; ++i)
        {
            if (i == 0 || i == m - 1)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (board[i][j] == 'O')
                        dfs(board, i, j);
                }
            }
            else
            {
                if (board[i][0] == 'O')
                    dfs(board, i, 0);
                if (board[i][n - 1] == 'O')
                    dfs(board, i, n - 1);
            }
        }
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
            {
                if (board[i][j] == 'A')
                    board[i][j] = 'O';
                else
                    board[i][j] = 'X';
            }
    }
    void dfs(vector<vector<char>> &board, int i, int j)
    {
        board[i][j] = 'A';
        if (i - 1 < m && i - 1 >= 0 && j < n && j >= 0 && board[i - 1][j] == 'O')
        {
            dfs(board, i - 1, j);
        }
        if (i + 1 < m && i + 1 >= 0 && j < n && j >= 0 && board[i + 1][j] == 'O')
        {
            dfs(board, i + 1, j);
        }
        if (i < m && i >= 0 && j - 1 < n && j - 1 >= 0 && board[i][j - 1] == 'O')
        {
            dfs(board, i, j - 1);
        }
        if (i < m && i >= 0 && j + 1 < n && j + 1 >= 0 && board[i][j + 1] == 'O')
        {
            dfs(board, i, j + 1);
        }
    }
};


```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```