-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLC0130.cpp
executable file
·50 lines (43 loc) · 1 KB
/
LC0130.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
/*
Problem Statement: https://leetcode.com/problems/surrounded-regions/
*/
class Solution {
public:
void solve(vector<vector<char>>& board) {
if (board.empty())
return;
int m, n;
m = board.size();
n = board[0].size();
// traverse neighbours easily
vector<int> xdir = {-1, 0, 1, 0};
vector<int> ydir = {0, -1, 0, 1};
// flood-fill
function<void(int, int)> dfs = [&](int i, int j) {
// base cases
if (i < 0 || i == m || j < 0 || j == n || board[i][j] != 'O')
return;
// visit position (i, j)
board[i][j] = '*';
for (int k = 0; k < xdir.size(); k++)
dfs(i + xdir[k], j + ydir[k]);
};
// go through the borders
for (int i = 0; i < n; i++) {
dfs(0, i);
dfs(m - 1, i);
}
for (int i = 0; i < m; i++) {
dfs(i, 0);
dfs(i, n - 1);
}
// capture surrounded regions
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O')
board[i][j] = 'X';
else if (board[i][j] == '*')
board[i][j] = 'O';
}
}
};