50分求助
查看原帖
50分求助
1394400
ZHOUZHEK楼主2024/12/13 14:43

rt

代码如下

#include<cstdio>
#include<iostream>
using namespace std;
int cell[201][201],ans=0,m,n;
const int x1[9]={-1,0,0,1};
const int y1[9]={0,-1,1,0};
void bfs(int x,int y) {
	int i;
	for (i=1;i<=4;i++) {
		if (x+x1[i]>0&&x+x1[i]<=n&&y+y1[i]>0&&y+y1[i]<=m&&cell[x+x1[i]][y+y1[i]]!=0) {
			cell[x+x1[i]][y+y1[i]]=0;
			bfs(x+x1[i],y+y1[i]);
		}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	char s;
	for (int i=1;i<=n;i++) {
		for (int j=1;j<=m;j++) {
			cin>>s;
			if (s>'0') cell[i][j]=1;
			else cell[i][j]=0;
		}
	}
	for (int i=1;i<=n;i++) {
		for (int j=1;j<=m;j++) {
			if (cell[i][j]!=0) {
				cell[i][j]=0;
				bfs(i,j);
				ans++;
			}
		}
	}
	printf("%d",ans);
	return 0;
}
2024/12/13 14:43
加载中...