你写的有点复杂~~看不懂 bushi)~~
by IOIexaminee @ 2024-03-20 21:29:39
$bfs$ 模板直接套就行
by IOIexaminee @ 2024-03-20 21:31:03
@[Unreal414](/user/764499)
```cpp
/*@[Unreal414](/user/764499) 版权所有!!!*/
#include <bits/stdc++.h>
using namespace std;
int n, m, result = 0, direction[4][2] = {{ 0, 1 }, { 0, -1 }, { -1, 0 }, { 1, 0 }};
char v[104][104];
bool visited[104][104] = { false };
queue <pair <int, int> > q;
void solve(int x, int y) {
q.push(make_pair(x, y));
visited[x][y] = true;
while (q.size()) {
int tx = q.front().first, ty = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int targetX = tx + direction[i][0], targetY = ty + direction[i][1];
if (targetX < 1 || targetX > n || targetY < 1 || targetY > m) continue;
if (visited[targetX][targetY] || v[targetX][targetY] == '0') continue;
q.push(make_pair(targetX, targetY));
visited[targetX][targetY] = true;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
v[i][j] = '0';
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cin >> v[i][j];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (!visited[i][j] && v[i][j] != '0') {
solve(i, j);
result++;
}
cout << result << "\n";
return 0;
}
```
by Li_Feiy @ 2024-03-20 21:44:25
@[Unreal414](/user/764499) bfs内部判边界的时候 `targetY > n` 改成 `targetY > m`
by Li_Feiy @ 2024-03-20 21:45:08
@[Li_Feiy](/user/941431) 谢谢大佬
by Unreal414 @ 2024-03-21 18:08:53