代码贴上来。
void DFS(vector > &board, int i, int j, int m, int n){
if (i < 0 || j < 0|| i >= m || j >= n || board[i][j] != 'O')
return;
board[i][j] = 'P';
DFS(board, i+1, j, m, n);
DFS(board, i-1, j, m, n);
DFS(board, i, j+1, m, n);
DFS(board, i, j-1, m, n);
}
void solve(vector> &board) {
if (board.empty())
return;
int m = board.size(), n = board[0].size();
for(int i = 0; i < m; i++){
DFS(board, i, 0, m, n);
DFS(board, i, n-1, m, n);
}
for(int j = 0; j < n; j++){
DFS(board, 0, j, m, n);
DFS(board, m-1, j, m, n);
}
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++){
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == 'P') board[i][j] = 'O';
}
}
};