生命体个数
2023-08-17 16:50:55
发布于:广东
WA了好多次
#include<bits/stdc++.h>
using namespace std;
int n, m, vis[5010][5010],dx[4] = {-1,1,0,0}, dy[4] = {0,0,-1,1}, t;
char a[5010][5010];
struct point{
int x, y;
};
void bfs(int x, int y){
queue<point> q;
q.push({x,y});
vis[x][y] = 1;
while(q.size()){
point top = q.front();
for(int i = 0; i < 4; i++){
int nx = top.x + dx[i], ny = top.y + dy[i];
if(nx > n || ny > m || nx < 1 || ny < 1 || vis[nx][ny] || a[nx][ny] == '0') continue;
q.push({nx, ny});
vis[nx][ny] = 1;
}
q.pop();
}
}
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 i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j] != '0' && vis[i][j] == 0){
bfs(i,j);
t++;
}
}
}
cout << t;
return 0;
}
这里空空如也
有帮助,赞一个