-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path130_Surrounded_regions
176 lines (171 loc) · 6.55 KB
/
130_Surrounded_regions
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
BFS
20ms 16%
class Solution {
private:
void BFS(vector<vector<char>> &board, int x, int y, char R, vector<vector<int>> &locations){
int n_rows = board.size();
int n_cols = board[0].size();
queue<vector<int>> q;
vector<int> current;
// initialization
q.push({x, y});
while(!q.empty()){
int size = q.size();
for (int i = 0; i < size; ++i){
// get current
current = q.front();
q.pop();
// check if it is 'O'
if (board[current[0]][current[1]] == 'O'){
board[current[0]][current[1]] = R;
locations.push_back(current);
// check neighbors
if (current[0] > 0 && board[current[0] - 1][current[1]] == 'O') q.push({current[0] - 1, current[1]});
if (current[1] > 0 && board[current[0]][current[1] - 1] == 'O') q.push({current[0], current[1] - 1});
if (current[0] <= n_rows - 2 && board[current[0] + 1][current[1]] == 'O') q.push({current[0] + 1, current[1]});
if (current[1] <= n_cols - 2 && board[current[0]][current[1] + 1] == 'O') q.push({current[0], current[1] + 1});
}
}
}
return;
}
public:
void solve(vector<vector<char>>& board) {
if (board.empty()) return;
int n_rows = board.size();
int n_cols = board[0].size();
if (n_rows <= 2 || n_cols <= 2) return;
// flip all boarder regions to "A", keep it in a space
vector<vector<int>> locations, temp;
// top & bottom
for (int i = 0; i < n_cols; ++i){
if (board[0][i] == 'O') BFS(board, 0, i, 'A', locations);
if (board[n_rows - 1][i] == 'O') BFS(board, n_rows - 1, i, 'A', locations);
}
// left & right
for (int i = 0; i < n_rows; ++i){
if (board[i][0] == 'O') BFS(board, i, 0, 'A', locations);
if (board[i][n_cols - 1] == 'O') BFS(board, i, n_cols - 1, 'A', locations);
}
// filp center
for (int i = 1; i < n_rows - 1; ++i){
for (int j = 1; j < n_cols - 1; ++j){
if (board[i][j] == 'O') BFS(board, i, j, 'X', temp);
}
}
// flip 'A's back
for (int i = 0; i < locations.size(); ++i){
board[locations[i][0]][locations[i][1]] = 'O';
}
return;
}
};
BFS
not searching for center
8ms over 90%
class Solution {
private:
void BFS(vector<vector<char>> &board, int x, int y, char R, vector<vector<int>> &locations){
int n_rows = board.size();
int n_cols = board[0].size();
queue<vector<int>> q;
vector<int> current;
// initialization
q.push({x, y});
while(!q.empty()){
int size = q.size();
for (int i = 0; i < size; ++i){
// get current
current = q.front();
q.pop();
// check if it is 'O'
if (board[current[0]][current[1]] == 'O'){
board[current[0]][current[1]] = R;
locations.push_back(current);
// check neighbors
if (current[0] > 0 && board[current[0] - 1][current[1]] == 'O') q.push({current[0] - 1, current[1]});
if (current[1] > 0 && board[current[0]][current[1] - 1] == 'O') q.push({current[0], current[1] - 1});
if (current[0] <= n_rows - 2 && board[current[0] + 1][current[1]] == 'O') q.push({current[0] + 1, current[1]});
if (current[1] <= n_cols - 2 && board[current[0]][current[1] + 1] == 'O') q.push({current[0], current[1] + 1});
}
}
}
return;
}
public:
void solve(vector<vector<char>>& board) {
if (board.empty()) return;
int n_rows = board.size();
int n_cols = board[0].size();
if (n_rows <= 2 || n_cols <= 2) return;
// flip all boarder regions to "A", keep it in a space
vector<vector<int>> locations;
// top & bottom
for (int i = 0; i < n_cols; ++i){
if (board[0][i] == 'O') BFS(board, 0, i, 'A', locations);
if (board[n_rows - 1][i] == 'O') BFS(board, n_rows - 1, i, 'A', locations);
}
// left & right
for (int i = 0; i < n_rows; ++i){
if (board[i][0] == 'O') BFS(board, i, 0, 'A', locations);
if (board[i][n_cols - 1] == 'O') BFS(board, i, n_cols - 1, 'A', locations);
}
// filp center
for (int i = 1; i < n_rows - 1; ++i){
for (int j = 1; j < n_cols - 1; ++j){
if (board[i][j] == 'O') board[i][j] = 'X';
}
}
// flip 'A's back
for (int i = 0; i < locations.size(); ++i){
board[locations[i][0]][locations[i][1]] = 'O';
}
return;
}
};
DFS
20ms
class Solution {
private:
vector<vector<int>> POS = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int n_rows, n_cols;
void DFS(vector<vector<char>> &board, int x, int y, char R, vector<vector<int>> &locations){
if (x < 0 || y < 0 || x >= n_rows || y >= n_cols || board[x][y] != 'O') return;
board[x][y] = R;
locations.push_back({x, y});
for (int i = 0; i < 4; ++i){
DFS(board, x + POS[i][0], y + POS[i][1], 'A', locations);
}
return;
}
public:
void solve(vector<vector<char>>& board) {
if (board.empty()) return;
n_rows = board.size();
n_cols = board[0].size();
if (n_rows <= 2 || n_cols <= 2) return;
// flip all boarder regions to "A", keep it in a space
vector<vector<int>> locations;
// top & bottom
for (int i = 0; i < n_cols; ++i){
if (board[0][i] == 'O') DFS(board, 0, i, 'A', locations);
if (board[n_rows - 1][i] == 'O') DFS(board, n_rows - 1, i, 'A', locations);
}
// left & right
for (int i = 0; i < n_rows; ++i){
if (board[i][0] == 'O') DFS(board, i, 0, 'A', locations);
if (board[i][n_cols - 1] == 'O') DFS(board, i, n_cols - 1, 'A', locations);
}
// filp center
for (int i = 1; i < n_rows - 1; ++i){
for (int j = 1; j < n_cols - 1; ++j){
if (board[i][j] == 'O') board[i][j] = 'X';
}
}
// flip 'A's back
for (int i = 0; i < locations.size(); ++i){
board[locations[i][0]][locations[i][1]] = 'O';
}
return;
}
};