-
Notifications
You must be signed in to change notification settings - Fork 0
/
130.surrounded-regions.cpp
70 lines (63 loc) · 2.1 KB
/
130.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
/*
* @lc app=leetcode id=130 lang=cpp
*
* [130] Surrounded Regions
*/
class Solution {
public:
int MOVE_X[4] = {-1,0,1,0};
int MOVE_Y[4] = {0,-1,0,1};
int rows, cols;
void solve(vector<vector<char>>& board) {
const int R = board.size();
if (R == 0)
return;
const int C = board[0].size();
if (C == 0)
return;
rows = R;
cols = C;
vector<bool> colors; //true = on the edge = not surrounded
int board_int[R][C];
for (int i = 0;i<R;i++)
for (int j = 0;j<C;j++)
board_int[i][j] = -1;
int cnt = 0;
for (int i = 0;i < R; i++)
for (int j = 0;j < C; j++)
{
if (board[i][j] == 'O' && board_int[i][j] == -1)
{
bool on_edge = false;
queue<vector<int>> q;
q.push({i,j});
vector<int> cur;
board_int[i][j] = cnt;
int new_X, new_Y;
while(!q.empty())
{
cur = q.front();
q.pop();
if (cur[0] == 0 || cur[0] == R-1 || cur[1] == 0 || cur[1] == C-1)
on_edge = true;
for(int k = 0;k < 4;k++)
{
new_X = cur[0] + MOVE_X[k];
new_Y = cur[1] + MOVE_Y[k];
if (new_X >= 0 && new_X < rows && new_Y >= 0 && new_Y < cols && board[new_X][new_Y] == 'O' && board_int[new_X][new_Y] == -1)
{
board_int[new_X][new_Y] = cnt;
q.push({new_X, new_Y});
}
}
}
colors.push_back(on_edge);
cnt++;
}
}
for (int i = 0;i < R; i++)
for (int j = 0;j < C; j++)
if (board[i][j] == 'O' && !colors[board_int[i][j]])
board[i][j] = 'X';
}
};