T2086.生命体个数
2023-08-07 14:57:52
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
struct node{int x,y;};
int n,m,ans,dx[4] = {-1,1,0,0},dy[4] = {0,0,-1,1};
char g[5010][5010];
void f(int nx,int ny){
queue<node> q;
q.push({nx,ny});
g[nx][ny] = 0;
while(!q.empty()){
node head = q.front();
q.pop();
for(int i = 0;i < 4;i++){
int tx = head.x + dx[i];
int ty = head.y + dy[i];
if(g[tx][ty] != '0' && tx >= 1 && tx <= n && ty >= 1 && ty <= m){
q.push({tx,ty});
g[tx][ty] = '0';
}
}
}
}
int main(){
cin>>n>>m;
for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) cin>>g[i][j];
for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) if(g[i][j] != '0') ans++,f(i,j);
cout<<ans;
return 0;
}
这里空空如也
有帮助,赞一个