Surrounded Regions Algorithm
The Surrounded Regions Algorithm is an efficient graph traversal technique that is employed to identify and modify the connected components in a given two-dimensional grid. The primary objective of this algorithm is to find the regions in the grid that are completely surrounded by a specific type of element, and replace them with another element. This algorithm is commonly applied to solve problems in the domains of image processing, game development, and geographic information systems, among others.
The Surrounded Regions Algorithm operates by initiating a depth-first search (DFS) or breadth-first search (BFS) traversal from the boundary cells of the grid. During the traversal, the algorithm examines the neighbors of each cell and checks if they are part of the surrounded region. If a neighbor cell belongs to the surrounded region, it is marked and added to the traversal stack or queue. Once all the connected components of the grid have been traversed, the algorithm proceeds to replace the marked cells with the desired element. By starting the traversal from the boundary cells, the Surrounded Regions Algorithm efficiently identifies and processes only those regions that are completely surrounded by the specified element, ensuring optimal performance and accuracy.
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
class Solution {
public:
void solve(vector<vector<char>> &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (board.empty()) return;
row = board.size();
col = board[0].size();
for (int i = 0; i < row; i++) {
if (board[i][0] == 'O') dfs(i, 0, board);
if (board[i][col-1] == 'O') dfs(i, col-1, board);
}
for (int j = 1; j < col; j++) {
if (board[0][j] == 'O') dfs(0, j, board);
if (board[row-1][j] == 'O') dfs(row-1, j, board);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'O') board[i][j] = 'X';
else if (board[i][j] == 'B') board[i][j] = 'O';
}
}
}
void dfs(int x, int y, vector<vector<char>> &board) {
board[x][y] = 'B';
for (int k = 0; k < 4; k++) {
int i = x + dx[k];
int j = y + dy[k];
if (check(i, j) && board[i][j] == 'O') {
dfs(i, j, board);
}
}
}
bool check(int x, int y) {
if (x >= 0 && x < row && y >= 0 && y < col) {
return true;
}
return false;
}
private:
int row, col;
};