代码
/*
* @lc app=leetcode.cn id=130 lang=cpp
*
* [130] 被围绕的区域
* flood fill
*/
// @lc code=start
class Solution {
public:
int n, m;
vector<vector<char>> board;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void solve(vector<vector<char>>& _board) {
board = _board;
n = board.size();
if (!n) return;
m = board[0].size();
for (int i = 0; i < n; i ++ ) {
if (board[i][0] == 'O') dfs(i, 0);
if (board[i][m - 1] == 'O') dfs(i, m - 1);
}
for (int i = 0; i < m; i ++ ) {
if (board[0][i] == 'O') dfs(0, i);
if (board[n - 1][i] == 'O') dfs(n - 1, i);
}
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (board[i][j] == '#') board[i][j] = 'O';
else board[i][j] = 'X';
_board = board;
}
void dfs(int x, int y) {
board[x][y] = '#';
for (int i = 0; i < 4; i ++ ) {
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < m && board[a][b] == 'O')
dfs(a, b);
}
}
};
// @lc code=end