Graphs · Pattern 5 of 14
The complement trick: search from the border
Finding what is trapped is hard; finding what escapes is easy. DFS from every border cell to mark the safe cells, then everything left unmarked is the answer.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- Cells that are fully surrounded, enclosed or cannot reach the edge
- Capture, flip or count regions that do not touch the border
The template
void solve(char[][] board) {
int m = board.length, n = board[0].length;
for (int r = 0; r < m; r++) { mark(board, r, 0); mark(board, r, n - 1); }
for (int c = 0; c < n; c++) { mark(board, 0, c); mark(board, m - 1, c); }
for (int r = 0; r < m; r++)
for (int c = 0; c < n; c++)
board[r][c] = board[r][c] == '#' ? 'O' : 'X'; // safe back to O, the rest is captured
}
// DFS from the border: every O reached here can escape
void mark(char[][] b, int r, int c) {
if (r < 0 || c < 0 || r >= b.length || c >= b[0].length || b[r][c] != 'O') return;
b[r][c] = '#';
mark(b, r + 1, c); mark(b, r - 1, c); mark(b, r, c + 1); mark(b, r, c - 1);
} def solve(board):
m, n = len(board), len(board[0])
def mark(r, c): # DFS from the border: these O's can escape
if r < 0 or c < 0 or r >= m or c >= n or board[r][c] != "O":
return
board[r][c] = "#"
mark(r + 1, c); mark(r - 1, c); mark(r, c + 1); mark(r, c - 1)
for r in range(m):
mark(r, 0); mark(r, n - 1)
for c in range(n):
mark(0, c); mark(m - 1, c)
for r in range(m):
for c in range(n): # safe back to O, the rest is captured
board[r][c] = "O" if board[r][c] == "#" else "X" // DFS from the border: every O reached here can escape
void mark(vector<vector<char>>& b, int r, int c) {
if (r < 0 || c < 0 || r >= (int)b.size() || c >= (int)b[0].size() || b[r][c] != 'O') return;
b[r][c] = '#';
mark(b, r + 1, c); mark(b, r - 1, c); mark(b, r, c + 1); mark(b, r, c - 1);
}
void solve(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size();
for (int r = 0; r < m; r++) { mark(board, r, 0); mark(board, r, n - 1); }
for (int c = 0; c < n; c++) { mark(board, 0, c); mark(board, m - 1, c); }
for (auto& row : board)
for (char& x : row) x = x == '#' ? 'O' : 'X'; // safe back to O, the rest is captured
} function solve(board) {
const m = board.length, n = board[0].length;
// DFS from the border: every O reached here can escape
const mark = (r, c) => {
if (r < 0 || c < 0 || r >= m || c >= n || board[r][c] !== 'O') return;
board[r][c] = '#';
mark(r + 1, c); mark(r - 1, c); mark(r, c + 1); mark(r, c - 1);
};
for (let r = 0; r < m; r++) { mark(r, 0); mark(r, n - 1); }
for (let c = 0; c < n; c++) { mark(0, c); mark(m - 1, c); }
for (let r = 0; r < m; r++)
for (let c = 0; c < n; c++) board[r][c] = board[r][c] === '#' ? 'O' : 'X'; // safe back to O
} The variations
-
1Mark what escapes, flip the rest
Every O connected to the border survives. Flood from the four edges and mark those cells, then one pass turns marked cells back into O and every other O into X.
Mistakes to avoid
- Flipping a region as soon as you find it, before you know whether it touches the border. Mark the safe cells first, flip at the end.
- Using a third marker that already appears in the input. Pick a character (like
#) the board never contains.
Mark it done once you can write the template from memory and you have solved the worked examples yourself.