-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode1254.cpp
301 lines (289 loc) · 9.12 KB
/
leetcode1254.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*************************************************
Author: wenhaofang
Date: 2022-11-12
Description: leetcode1254 - Number of Closed Islands
*************************************************/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
using std::vector;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::stack;
using std::queue;
using std::priority_queue;
using std::max;
using std::min;
using std::swap;
using std::pair;
using std::cin;
using std::cout;
using std::endl;
/**
* 方法一:深度优先搜索
*
* 理论时间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
* 理论空间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
*
* 实际时间复杂度:Runtime: 18 ms, faster than 82.46% of C++ online submissions
* 实际空间复杂度:Memory Usage: 9.4 MB, less than 93.63% of C++ online submissions
*/
// class Solution {
// public:
// void dfs(vector<vector<int>>& grid, int x, int y, int m, int n) {
// if (
// x < 0 ||
// y < 0 ||
// x >= m ||
// y >= n ||
// grid[x][y] != 0
// ) {
// return;
// }
// grid[x][y] = 1;
// dfs(grid, x + 1, y, m, n);
// dfs(grid, x - 1, y, m, n);
// dfs(grid, x, y + 1, m, n);
// dfs(grid, x, y - 1, m, n);
// }
// int closedIsland(vector<vector<int>>& grid) {
// // 特判
// int m = grid.size();
// if (m <= 2) {
// return 0;
// }
// int n = grid[0].size();
// if (n <= 2) {
// return 0;
// }
// // 1. 找出边界上的 0
// // 2. 找出与上述的 0 相连的 0 (深度优先搜索)
// // 3. 将上述找到的 0 填充为 1
// for (int i = 1; i <= m - 2; i++) {
// if (grid[i][0] == 0) {
// dfs(grid, i, 0, m, n);
// }
// if (grid[i][n - 1] == 0) {
// dfs(grid, i, n - 1, m, n);
// }
// }
// for (int j = 0; j <= n - 1; j++) {
// if (grid[0][j] == 0) {
// dfs(grid, 0, j, m, n);
// }
// if (grid[m - 1][j] == 0) {
// dfs(grid, m - 1, j, m, n);
// }
// }
// // 4. 找出矩阵中的 0
// // 5. 找出与上述的 0 相连的 0 (深度优先搜索)
// // 6. 将上述找到的 0 填充为 1
// // 7. 答案加一
// int ans = 0;
// for (int i = 1; i < m - 1; i++) {
// for (int j = 1; j < n - 1; j++) {
// if (grid[i][j] == 0) {
// dfs(grid, i, j, m, n);
// ans += 1;
// }
// }
// }
// return ans;
// }
// };
/**
* 方法二:广度优先搜索
*
* 理论时间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
* 理论空间复杂度:O(m * n),其中 m、n 分别为矩阵的宽和长
*
* 实际时间复杂度:Runtime: 35 ms, faster than 22.50% of C++ online submissions
* 实际空间复杂度:Memory Usage: 13.9 MB, less than 9.37% of C++ online submissions
*/
// class Solution {
// public:
// const int dx[4] = {1, -1, 0, 0};
// const int dy[4] = {0, 0, 1, -1};
// void bfs(vector<vector<int>>& grid, int x, int y, int m, int n) {
// queue<pair<int, int>> q;
// grid[x][y] = 1;
// q.emplace(x, y);
// while (!q.empty()) {
// int nx = q.front().first;
// int ny = q.front().second;
// q.pop();
// for (int k = 0; k < 4; k++) {
// int mx = nx + dx[k];
// int my = ny + dy[k];
// if (
// mx < 0 ||
// my < 0 ||
// mx >= m ||
// my >= n ||
// grid[mx][my] != 0
// ) {
// continue;
// }
// grid[mx][my] = 1;
// q.emplace(mx, my);
// }
// }
// }
// int closedIsland(vector<vector<int>>& grid) {
// // 特判
// int m = grid.size();
// if (m <= 2) {
// return 0;
// }
// int n = grid[0].size();
// if (n <= 2) {
// return 0;
// }
// // 1. 找出边界上的 0
// // 2. 找出与上述的 0 相连的 0 (广度优先搜索)
// // 3. 将上述找到的 0 填充为 1
// for (int i = 1; i <= m - 2; i++) {
// if (grid[i][0] == 0) {
// bfs(grid, i, 0, m, n);
// }
// if (grid[i][n - 1] == 0) {
// bfs(grid, i, n - 1, m, n);
// }
// }
// for (int j = 0; j <= n - 1; j++) {
// if (grid[0][j] == 0) {
// bfs(grid, 0, j, m, n);
// }
// if (grid[m - 1][j] == 0) {
// bfs(grid, m - 1, j, m, n);
// }
// }
// // 4. 找出矩阵中的 0
// // 5. 找出与上述的 0 相连的 0 (广度优先搜索)
// // 6. 将上述找到的 0 填充为 1
// // 7. 答案加一
// int ans = 0;
// for (int i = 1; i < m - 1; i++) {
// for (int j = 1; j < n - 1; j++) {
// if (grid[i][j] == 0) {
// bfs(grid, i, j, m, n);
// ans += 1;
// }
// }
// }
// return ans;
// }
// };
class Solution {
public:
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int closedIsland(vector<vector<int>>& grid) {
// 特判
int m = grid.size();
if (m <= 2) {
return 0;
}
int n = grid[0].size();
if (n <= 2) {
return 0;
}
// 1. 找出边界上的 0
// 2. 找出与上述的 0 相连的 0 (广度优先搜索)
// 3. 将上述找到的 0 填充为 1
queue<pair<int, int>> q1;
for (int i = 1; i <= m - 2; i++) {
if (grid[i][0] == 0) {
q1.emplace(i, 0);
}
if (grid[i][n - 1] == 0) {
q1.emplace(i, n - 1);
}
}
for (int j = 0; j <= n - 1; j++) {
if (grid[0][j] == 0) {
q1.emplace(0, j);
}
if (grid[m - 1][j] == 0) {
q1.emplace(m - 1, j);
}
}
while (!q1.empty()) {
int nx = q1.front().first;
int ny = q1.front().second;
q1.pop();
for (int k = 0; k < 4; k++) {
int mx = nx + dx[k];
int my = ny + dy[k];
if (
mx < 0 ||
my < 0 ||
mx >= m ||
my >= n ||
grid[mx][my] != 0
) {
continue;
}
grid[mx][my] = 1;
q1.emplace(mx, my);
}
}
// 4. 找出矩阵中的 0
// 5. 找出与上述的 0 相连的 0 (广度优先搜索)
// 6. 将上述找到的 0 填充为 1
// 7. 答案加一
int ans = 0;
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (grid[i][j] == 0) {
ans += 1;
queue<pair<int, int>> q2;
q2.emplace(i, j);
while (!q2.empty()) {
int nx = q2.front().first;
int ny = q2.front().second;
q2.pop();
for (int k = 0; k < 4; k++) {
int mx = nx + dx[k];
int my = ny + dy[k];
if (
mx < 0 ||
my < 0 ||
mx >= m ||
my >= n ||
grid[mx][my] != 0
) {
continue;
}
grid[mx][my] = 1;
q2.emplace(mx, my);
}
}
}
}
}
return ans;
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
vector<vector<int>> grid = {
{1, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 1, 1, 0},
{1, 0, 1, 0, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 0},
};
int ans = solution -> closedIsland(grid);
cout << ans << endl;
}