社区讨论

求助

P1451求细胞数量参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lo1rw6bs
此快照首次捕获于
2023/10/23 01:58
2 年前
此快照最后确认于
2023/11/03 02:36
2 年前
查看原帖
CPP
#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;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...