0%

### 题目描述

Given an `m x n` matrix 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.

### 例子

#### 例子 1

Input:
`board =`
`[["X","X","X","X"],`
`["X","O","O","X"],`
`["X","X","O","X"],`
`["X","O","X","X"]]`
Output:
`[["X","X","X","X"],`
`["X","X","X","X"],`
`["X","X","X","X"],`
`["X","O","X","X"]]`
Explanation:Surrounded regions should not 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.

### Constraints

• `m == board.length`
• `n == board[i].length`
• `1 <= m, n <= 200`
• `board[i][j] is 'X' or 'O'`

### 解题思路

[Leetcode]200. Number of Islands(C++) 的思路类似，用 dfs 来对区域进行搜索。在搜索的同时需要对当前格子进行判断是否在边缘如果在则表明当前区域不需要更改，因此还需要用一个列表来维护当前区域包含的格子。

• 时间复杂度: `O(mn)`
• 空间复杂度`O(mn)`

GitHub 代码同步地址： 130.SurroundedRegions.cpp

GitHub: Leetcode-C++-Solution