求助
查看原帖
求助
668868
lisuhengyu0617楼主2023/8/19 19:20
#include<bits/stdc++.h>
using namespace std;
char a[100005][100005];
int vis[100005][100005];
int n,m;
struct node{
	int x,y;
};
bool check(int x,int y){
	if(x>=1&&x<=n&&y>=1&&y<=m&&vis[x][y]==0&&a[x][y]!='0'){
		return true;
	}
	return false;
}
void bfs(int x0,int y0){
	vis[x0][y0]=1;
	queue<node> q;
	q.push({x0,y0});
	while(!q.empty()){
		node t=q.front();
		q.pop();
		int x=t.x;
		int y=t.y;
		if(check(x-1,y)){
			q.push({x-1,y});
			vis[x-1][y]=1;
		}
		if(check(x+1,y)){
			q.push({x+1,y});
			vis[x+1][y]=1;
		}
		if(check(x,y-1)){
			q.push({x,y-1});
			vis[x][y-1]=1;
		}
		if(check(x,y+1)){
			q.push({x,y+1});
			vis[x][y+1]=1;
		}
	}
} 
int main(){
	int ans=0;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(check(i,j)){
				bfs(i,j);
				ans++;
			}
		}
	}
	cout<<ans;
}
2023/8/19 19:20
加载中...