!-- 自定制样式文件 -->

细胞个数

 

 一个要用连通块和bfs做的题,如果他的上下左右有零的话则不是

上代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <queue>
using namespace std;
char c[10005][10005];
int fx[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n,m,z,a[10005][10005],v[10005][10005],ans,s,maxx=-1000,bz;
void dfs (int x,int y){

s++;
a[x][y]=1;
cout<<x<<" "<<y<<" "<<s<<endl;
for(int i=0;i<4;i++){
int tx=fx[i][0]+x;
int ty=fx[i][1]+y;
if(tx<n && tx>=0 && ty<m && ty>=0 && v[tx][ty]==1 && a[tx][ty]==0){
dfs(tx,ty);
}
}
}

int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>c[i][j];
if(c[i][j]!='0')v[i][j]=1;
else v[i][j]=0;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(v[i][j]==1 && a[i][j]==0){ //从每个点出发,s记录块常数,ans记录几个块
bz=1;
ans++; //连通块数 ,几个连通块
s=0;
dfs(i,j);
maxx=max(maxx,s); //最大的连通块
}
}
}
cout<<maxx<<" "<<ans;
return 0;
}

 

posted @ 2020-08-14 21:50  XzhouxxX  阅读(119)  评论(0编辑  收藏  举报