Skip to main content

Surrounded Regions

KANISHKA GUPTA
EditReport

Description:​

Given an m x n matrix board containing 'X' and 'O', capture all regions that are 4-directionally surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.


Video Solution:​


Approaches:​

1. Depth-First Search (DFS) on Boundaries (Optimal)​

This problem can be tricky if we try to find surrounded regions directly. Instead, it is much easier to identify the unsurrounded regions. Any 'O' that is connected to the boundary of the board cannot be surrounded by 'X's.

We can solve this using a reverse-thinking approach:

  1. Traverse the boundaries (first row, last row, first column, last column) of the board.
  2. If we find an 'O' on the boundary, we launch a DFS from that cell. The DFS will visit all adjacent 'O's and mark them as "safe" (e.g., by temporarily flipping them to a special character like 'T').
  3. Once the boundary traversal is complete, any 'O' remaining on the board is strictly surrounded by 'X's.
  4. Iterate over the entire board one last time:
    • Flip every remaining 'O' to 'X' (these are the captured regions).
    • Flip every 'T' back to 'O' (these are the safe regions that were connected to the boundary).

Complexity​

  • Time Complexity: O(M×N)O(M \times N) where MM is the number of rows and NN is the number of columns. We visit each cell at most a constant number of times.
  • Space Complexity: O(M×N)O(M \times N) in the worst-case scenario for the recursion call stack (e.g., if the entire board is filled with 'O's).

Solutions​

class Solution {
private:
void dfs(vector<vector<char>>& board, int r, int c) {
// Boundary check and look for 'O'
if (r < 0 || c < 0 || r >= board.size() || c >= board[0].size() || board[r][c] != 'O') {
return;
}

// Mark as a safe, unsurrounded region
board[r][c] = 'T';

// Traverse all 4 adjacent directions
dfs(board, r - 1, c);
dfs(board, r + 1, c);
dfs(board, r, c - 1);
dfs(board, r, c + 1);
}

public:
void solve(vector<vector<char>>& board) {
if (board.empty()) return;

int m = board.size();
int n = board[0].size();

// Traverse first and last columns
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);
}

// Traverse first and last rows
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);
}

// Process the board
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X'; // Captured
} else if (board[i][j] == 'T') {
board[i][j] = 'O'; // Safe
}
}
}
}
};
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

đŸ’Ŧ Discuss this page

Have a question or spot something confusing in "Surrounded Regions"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.