4
int visited[201][201] = {0};5
// Breadth First Search6
// Flood Fill Algorithm7
void bfs(vector<vector<char>> &board, int x, int y) {18
if (i > 0 && !visited[i - 1][j] && board[i - 1][j] == 'C') {19
visited[i - 1][j] = 1;20
q.push(m * (i - 1) + j);22
if (i < n - 1 && !visited[i + 1][j] && board[i + 1][j] == 'C') {23
visited[i + 1][j] = 1;24
q.push(m * (i + 1) + j);26
if (j > 0 && !visited[i][j - 1] && board[i][j - 1] == 'C') {27
visited[i][j - 1] = 1;28
q.push(m * i + j - 1);30
if (j < m - 1 && !visited[i][j + 1] && board[i][j + 1] == 'C') {31
visited[i][j + 1] = 1;32
q.push(m * i + j + 1);36
void solve(vector<vector<char>> &board) {39
// Marking the regions to capture40
// mark all the O to C41
for (int i = 0; i < n; i++) {42
for (int j = 0; j < m; j++) {43
if (board[i][j] == 'O') board[i][j] = 'C';46
// if we have found C on the outer edges, then all the connected C to it47
// should be converted to O as they can't be captured48
for (int i = 0; i < m; i++) {49
if (board[0][i] == 'C' && !visited[0][i]) bfs(board, 0, i);50
if (board[n - 1][i] == 'C' && !visited[n - 1][i]) bfs(board, n - 1, i);52
for (int i = 1; i < n - 1; i++) {53
if (board[i][0] == 'C' && !visited[i][0]) bfs(board, i, 0);54
if (board[i][m - 1] == 'C' && !visited[i][m - 1]) bfs(board, i, m - 1);56
// capturing the regions57
// now remaining C can be capture58
for (int i = 0; i < n; i++) {59
for (int j = 0; j < m; j++) {60
if (board[i][j] == 'C') board[i][j] = 'X';65
static const auto speedup = []() {66
std::ios::sync_with_stdio(false);67
std::cin.tie(nullptr);68
std::cout.tie(nullptr);