forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0130-surrounded-regions.cpp
110 lines (92 loc) · 2.94 KB
/
0130-surrounded-regions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Given a matrix, capture ('X') all regions that are surrounded ('O')
Distinguish captured vs escaped, 'X' vs 'O' vs 'E'
Time: O(m x n)
Space: O(m x n)
*/
class Solution {
public:
void solve(vector<vector<char>>& board) {
int m = board.size();
int n = board[0].size();
// marking escaped cells along the border
for (int i = 0; i < m; i++) {
dfs(board,i,0,m,n);
dfs(board,i,n-1,m,n);
}
for (int j = 0; j < n; j++) {
dfs(board,0,j,m,n);
dfs(board,m-1,j,m,n);
}
// flip cells to correct final states
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
}
if (board[i][j] == 'E') {
board[i][j] = 'O';
}
}
}
}
private:
void dfs(vector<vector<char>>& board, int i, int j, int m, int n) {
if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] != 'O') {
return;
}
board[i][j] = 'E';
dfs(board, i - 1, j, m, n);
dfs(board, i + 1, j, m, n);
dfs(board, i, j - 1, m, n);
dfs(board, i, j + 1, m, n);
}
};
/*
BFS Solution
*/
class Solution {
private:
int rows, cols;
void bfs(int row, int col, vector<vector<char>>& board) {
board[row][col] = 'E';
queue<pair<int, int>> q;
q.push({row, col});
vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
for (const auto &direction : directions) {
int newRow = r + direction.first;
int newCol = c + direction.second;
if (newRow < rows && newRow >= 0 && newCol < cols && newCol >= 0 && board[newRow][newCol] == 'O') {
board[newRow][newCol] = 'E';
q.push({newRow, newCol});
}
}
}
}
public:
void solve(vector<vector<char>>& board) {
rows = board.size();
cols = board[0].size();
for (int row = 0; row < rows; ++row) {
if (board[row][0] == 'O') bfs(row, 0, board);
if (board[row][cols - 1] == 'O') bfs(row, cols - 1, board);
}
for (int col = 0; col < cols; ++col) {
if (board[0][col] == 'O') bfs(0, col, board);
if (board[rows - 1][col] == 'O') bfs(rows - 1, col, board);
}
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
if (board[row][col] == 'O') {
board[row][col] = 'X';
}
else if (board[row][col] == 'E') {
board[row][col] = 'O';
}
}
}
}
};