@[jngrkek11](/user/1157267) 可以参考一下我的
(不要在意是BFS)
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
char a[105][105];
bool vis[105][105];
struct Pos {
int x, y;
Pos(int ax = 0, int ay = 0) {
x=ax; // 该结点的 x 值初始化为 ax
y=ay; // 该结点的 y 值初始化为 ay
}
};
// 从 (x, y) 开始 BFS 整个细胞
void bfs(int x, int y) {
queue<Pos> q;
q.push(Pos(x, y)); // 将 (x,y) 入队
while(!q.empty()) { // 当队列非空
Pos now = q.front(); // 现在处理队首结点
q.pop(); // 队首出队
int x = now.x, y = now.y;
if(x < 1 || x > n) continue;
if(y < 1 || y > m) continue;
if(a[x][y] == '0') continue; // 不是细胞点
if(vis[x][y]==1) continue;; // 如果这个点被访问过则跳过
vis[x][y]=1; // 用 vis 数组避免重复访问
q.push(Pos(x+1, y)); // 将上方结点加入到队列
q.push(Pos(x-1, y)); // 将下方结点加入到队列
q.push(Pos(x, y-1)); // 将左方结点加入到队列
q.push(Pos(x, y+1)); // 将右方结点加入到队列
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> a[i][j];
for(int x = 1; x <= n; x++)
for(int y = 1; y <= m; y++)
if(a[x][y]!='0' && vis[x][y]==0) { // (x,y) 这个点是细胞点,且未访问过
ans++;
bfs(x,y);// 开始对 (x,y) 这个点进行 BFS
}
cout << ans << endl;
return 0;
}
```
**别抄**
by HAha201205221633 @ 2024-04-09 19:02:24