题解:P9243 [蓝桥杯 2023 省 B] 岛屿个数

思路

第一时间想到 BFS 来统计岛屿数量。这是一篇比较直观但代码不够简洁的题解。

这道题重点在于如何区分岛屿和子岛屿。题目定义:如果一个岛屿 B 完全位于另一个岛屿 A 的某个“环”内部,则 B 是 A 的子岛屿,且不计入总数。

这里的“环”是指由陆地组成的闭合回路,而判断一个岛屿是否在环内,本质上等价于判断它的所有格子是否不与外海相连。

因此,我们需要区分外海(与地图外界相连的海水)和内海(被陆地包围、不与外界相通的海水)。

只有与外海直接相连的岛屿才是“外层岛屿”,需要被计入答案;完全被陆地包围的子岛屿,即使内部有海水,也不应被统计。

为了方便可以在地图外填充一圈海水,和外海一样标记为 s(代表外海)。这样,所有与外界相通的海水都可以通过这一圈格子扩散得到。

注意到海是八方向连续的。

例子(样例第二组):

1
2
3
4
5
6
7
ssssssss
s111111s
s100001s
ss10101s
s100001s
s111111s
ssssssss

地图内的外海只有 $(3,1)$ 一个格子。

所以这一段标记外海的 BFS 是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void bfs1 () {
queue<pair<int, int> > q;
for (int i = 0; i <= M + 1; i ++) {
for (int j = 0; j <= N + 1; j ++) {
if (i == 0 || j == 0 || i == M + 1 || j == N + 1) {
a[i][j] = 's';
q.emplace (i, j);
}
}
}
// 以上是把地图外我们加上的边界放进队列,通过这些位置来找与之相接的、地图内的外海。好处是可以把外海连成一片。
while (!q.empty ()) { // 八方向扩散,将与外海相连的'0'都标记为's'。
auto [x, y] = q.front ();
q.pop ();
for (int i = 0; i < 8; i ++) { // 方向遍历。
int X = x + fx[i], Y = y + fy[i];
if (X >= 1 && Y >= 1 && X <= M && Y <= N && a[X][Y] == '0'){
q.emplace (X, Y);
a[X][Y] = 's'; // 放进去之后改为's',不会重复进队列。
}
}
}
}

接下来需要找到所有与外海相接的岛屿,并统计连通块个数。

从外海的某个格子开始搜索:

  • 如果遇到外海(s),继续扩散(标记为 S 避免重复)。
  • 如果遇到陆地(1),说明找到了一个与外海相连的岛屿,将其计数,并用四方向 BFS 将整个岛屿标记为 2(表示已统计过)。

注意岛屿是四方向连续的。不然会错误地将不同岛屿连接起来并与海水交叉。

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
void bfs3 (int x, int y) { // 搜索岛屿
queue<pair<int, int> > q3;
q3.emplace (x, y);
while (!q3.empty ()) {
auto [x, y] = q3.front ();
q3.pop ();
for (int i = 0; i < 4; i ++) {
int X = x + fx2[i], Y = y + fy2[i];
if (X >= 0 && Y >= 0 && X <= M + 1 && Y <= N + 1 && a[X][Y] == '1') {
a[X][Y] = '2'; // 标记为已访问。
q3.emplace (X, Y);
}
}
}
}
void bfs2 () { // 寻找岛屿
queue<pair<int, int> > q2;
q2.emplace (0, 0);
while (!q2.empty ()) {
auto [x, y] = q2.front ();
q2.pop ();
for (int i = 0; i < 8; i ++) {
int X = x + fx[i], Y = y + fy[i];
if (X >= 0 && Y >= 0 && X <= M + 1 && Y <= N + 1) {
if (a[X][Y] == 's') {
a[X][Y] = 'S'; // 标记已访问的外海。
q2.emplace (X, Y);
} // 当一个
else if (a[X][Y] == '1') {
cnt ++; // 找到一个外层的岛屿。
a[X][Y] = '2';
bfs3 (X, Y); // 标记整个岛屿。
}
}
}
}
}

完整代码

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
#include <bits/stdc++.h>
using namespace std;
char a[55][55];
int M, N, cnt;
int fx[] = {1, 1, 1, 0, -1, -1, -1, 0}, fy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int fx2[] = {1, 0, -1, 0}, fy2[] = {0, 1, 0, -1};
void bfs1 () {
queue<pair<int, int> > q;
for (int i = 0; i <= M + 1; i ++) {
for (int j = 0; j <= N + 1; j ++) {
if (i == 0 || j == 0 || i == M + 1 || j == N + 1) {
a[i][j] = 's';
q.emplace (i, j);
}
}
}
while (!q.empty ()) {
auto [x, y] = q.front ();
q.pop ();
for (int i = 0; i < 8; i ++) {
int X = x + fx[i], Y = y + fy[i];
if (X >= 1 && Y >= 1 && X <= M && Y <= N && a[X][Y] == '0'){
q.emplace (X, Y);
a[X][Y] = 's';
}
}
}
}
void bfs3 (int x, int y) {
queue<pair<int, int> > q3;
q3.emplace (x, y);
while (!q3.empty ()) {
auto [x, y] = q3.front ();
q3.pop ();
for (int i = 0; i < 4; i ++) {
int X = x + fx2[i], Y = y + fy2[i];
if (X >= 0 && Y >= 0 && X <= M + 1 && Y <= N + 1 && a[X][Y] == '1') {
a[X][Y] = '2';
q3.emplace (X, Y);
}
}
}
}
void bfs2 () {
queue<pair<int, int> > q2;
q2.emplace (0, 0);
while (!q2.empty ()) {
auto [x, y] = q2.front ();
q2.pop ();
for (int i = 0; i < 8; i ++) {
int X = x + fx[i], Y = y + fy[i];
if (X >= 0 && Y >= 0 && X <= M + 1 && Y <= N + 1) {
if (a[X][Y] == 's') {
a[X][Y] = 'S';
q2.emplace (X, Y);
}
else if (a[X][Y] == '1') {
cnt ++;
a[X][Y] = '2';
bfs3 (X, Y);
}
}
}
}
}
void init (int M, int N) {
cnt = 0;
for (int i = 0; i <= M + 1; i ++) {
for (int j = 0; j <= N + 1; j ++) {
a[i][j] = 's';
}
}
}
int main () {
int T;
cin >> T;
while (T --) {
cin >> M >> N;
init (M, N);
for (int i = 1; i <= M; i ++) {
for (int j = 1; j <= N; j ++) {
cin >> a[i][j];
}
}
// 灌水
bfs1 ();
bfs2 ();
// for (int i = 0; i <= M + 1; i ++) {
// for (int j = 0; j <= N + 1; j ++) {
// cout << a[i][j];
// }
// cout << endl;
// }
cout << cnt << endl;
}
return 0;
}