Algo-Sim › Grid Graphs
Surrounded Regions
Medium · Grid Graphs · step through it in Java, Python and C++ on your own input
Capture every region of O that is completely surrounded by X (cannot reach the border) by flipping it to X.
Problem
The board holds X and O. A region of Os (joined horizontally or vertically) is captured when none of its cells is on the border, and capturing turns it into X. Capture every such region in place.
Constraints
- 1 ≤ m, n ≤ 200
- board[i][j] is 'X' or 'O'
- Easier in reverse: mark the O regions that touch the border, flip the rest
Edge cases to test
- Every O on the border
- A board of one row
- No O at all
Follow-up questions
- Why start from the border?
- Regions that touch the border are exactly the ones that survive; marking them first leaves only captured cells.
- What is the union-find alternative?
- Union border O cells with a virtual node, union neighbours, and flip cells not joined to it.
Solution
Java
public void solve(char[][] board) {
int rows = board.length, cols = board[0].length;
for (int r = 0; r < rows; r++) {
dfs(board, r, 0);
dfs(board, r, cols - 1);
}
for (int c = 0; c < cols; c++) {
dfs(board, 0, c);
dfs(board, rows - 1, c);
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (board[r][c] == 'O') board[r][c] = 'X';
else if (board[r][c] == 'S') board[r][c] = 'O';
}
}
}
private void dfs(char[][] board, int r, int c) {
if (r < 0 || c < 0 || r >= board.length || c >= board[0].length || board[r][c] != 'O') return;
board[r][c] = 'S';
dfs(board, r + 1, c);
dfs(board, r - 1, c);
dfs(board, r, c + 1);
dfs(board, r, c - 1);
}Python
def solve(board):
rows, cols = len(board), len(board[0])
for r in range(rows):
dfs(board, r, 0)
dfs(board, r, cols - 1)
for c in range(cols):
dfs(board, 0, c)
dfs(board, rows - 1, c)
for r in range(rows):
for c in range(cols):
if board[r][c] == "O":
board[r][c] = "X"
elif board[r][c] == "S":
board[r][c] = "O"
def dfs(board, r, c):
if not (0 <= r < len(board) and 0 <= c < len(board[0])) or board[r][c] != "O":
return
board[r][c] = "S"
dfs(board, r + 1, c)
dfs(board, r - 1, c)
dfs(board, r, c + 1)
dfs(board, r, c - 1)C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void solve(vector<vector<char>> &board) {
int rows = int(board.size()), cols = int(board[0].size());
for (int r = 0; r < rows; r++) {
dfs(board, r, 0);
dfs(board, r, cols - 1);
}
for (int c = 0; c < cols; c++) {
dfs(board, 0, c);
dfs(board, rows - 1, c);
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (board[r][c] == 'O')
board[r][c] = 'X';
else if (board[r][c] == 'S')
board[r][c] = 'O';
}
}
}
void dfs(vector<vector<char>> &board, int r, int c) {
if (r < 0 || c < 0 || r >= int(board.size()) || c >= int(board[0].size()) || board[r][c] != 'O')
return;
board[r][c] = 'S';
dfs(board, r + 1, c);
dfs(board, r - 1, c);
dfs(board, r, c + 1);
dfs(board, r, c - 1);
}
};
More Grid Graphs problems
- Flood Fill (Easy)
- Number of Islands (BFS) (Medium)
- Number of Islands (DFS) (Medium)
- Max Area of Island (Medium)
- Rotting Oranges (Medium)
- 01 Matrix (Medium)
- Walls and Gates (Medium)
- Shortest Path in Binary Matrix (Medium)
- Shortest Bridge (Medium)
- Shortest Path in a Grid with Obstacles Elimination (Hard)
- Pacific Atlantic Water Flow (Medium)
- Path With Minimum Effort (Medium)
- Minimum Cost to Make at Least One Valid Path in a Grid (Hard)
- Swim in Rising Water (Hard)