# LeetCode Surrounded Regions

LeetCode 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.

For 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```

```vector<vector<int>> dirs = { {1,0},{-1,0},{0,1},{0,-1} };

class Solution {
private:
int m, n;
bool isOk(int x, int y) {
return x >= 0 && x < m&&y >= 0 && y < n;
}
void dfs(vector<vector<char>>& board, int x, int y) {
board[x][y] = '1';
for (int i = 0; i < dirs.size(); ++i) {
int u = x + dirs[i][0], v = y + dirs[i][1];
if (isOk(u, v) && board[u][v] == 'O') dfs(board, u, v);
}
}
public:
void solve(vector<vector<char>>& board) {
if (board.empty() || board[0].empty())return;
m = board.size(), n = board[0].size();
for (int i = 0; i < m; ++i) {
if (board[i][0] == 'O') dfs(board, i, 0);
if (board[i][n - 1] == 'O') dfs(board, i, n - 1);
}
for (int j = 0; j < n; ++j) {
if (board[0][j] == 'O') dfs(board, 0, j);
if (board[m - 1][j] == 'O') dfs(board, m - 1, j);
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'O')board[i][j] = 'X';
else if (board[i][j] == '1')board[i][j] = 'O';
}
}
}
};
```