细胞
2024-12-14 20:23:54
发布于:江苏
1阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
char a[1001][1001];
void bfs(int x,int y){
if(x>n||x<1||y>m||y<1||a[x][y]=='0'){
return ;
}
a[x][y]='0';
bfs(x+1,y);
bfs(x,y+1);
bfs(x-1,y);
bfs(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 i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]!='0'){
ans++;
bfs(i,j);
}
}
}
cout<<ans;
return 0;
}
这里空空如也
有帮助,赞一个