#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0,a[120][120];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool check(int x,int y)
{
if(x>0&&x<=n&&y>0&&y<=m)return true;
else return false;
}
struct pace{
int qi,qj;
};
queue<pace>q;
void bfs(int i,int j)
{
a[i][j]=0;
pace now;
now.qi=i;
now.qj=j;
q.push(now);
while(!q.empty())
{
int x1=q.front().qi;
int y1=q.front().qj;
q.pop();
for(int i=0;i<4;i++)
{
pace next;
int nx=x1+dir[i][0];
int ny=y1+dir[i][1];
if(check(nx,ny)&&a[nx][ny])
{
a[nx][ny]=0;
next.qi=nx;
next.qj=ny;
q.push(next);
}
}
}
}
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])
{
bfs(i,j);
ans++;
}
}
}
cout<<ans;
return 0;
}
连通块
最新推荐文章于 2024-05-16 18:11:19 发布