求调
查看原帖
求调
830990
roumeideclown楼主2023/5/7 21:32
#include<bits/stdc++.h>
#pragma GCC optimeze(1)
#pragma GCC optimeze(2)
#pragma GCC optimeze(3,"Ofast","inline")
using namespace std;
struct nodes {
	int x,y;
};
nodes node;
string s;
int n,m,deltaX[5]={0,1,-1,0,0},deltaY[5]={0,0,0,1,-1};
bool matrix[201][201],inq[201][201],ans;
bool check(int x,int y) {
	if(x>=n||x<1||y>=m||y<1||matrix[x][y]==false||inq[x][y]==true) {
		return false;
	}
	return true;
}
void bfs(int x,int y) {
	queue<nodes> q;
	node.x=x;
	node.y=y;
	q.push(node);
	inq[x][y]=true;
	while(!q.empty()) {
		nodes top=q.front();
		q.pop();
		for(int i=1;i<=4;i++) {
			int nx=deltaX[i];
			int ny=deltaY[i];
			if(check(nx,ny)) {
				node.x=nx;
				node.y=ny;
				q.push(node);
				inq[nx][ny]=true;
			}
		}
	}
}
int main() {
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) {
		cin>>s;
		for(int j=1;j<=m;j++) {
			int x=s[j-1]-'0';
			if(x>0) {
				matrix[i][j]=true;
			}
		}
	}
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			if(matrix[i][j]==true&&inq[i][j]==false) {
				ans++;
				bfs(i,j);
			}
		}
	}
	printf("%d",ans);
	return 0;
}

2023/5/7 21:32
加载中...