求助,样例过不了(玄关)
查看原帖
求助,样例过不了(玄关)
1125129
WJX120423楼主2024/6/27 16:41
#include<bits/stdc++.h>
using namespace std;
const int maxn=510;
int cnt=0;
char s[maxn][maxn];
bool vis[maxn][maxn];
int n,m;
struct node{
	int x,y;
	node(int xx,int yy){
		x=xx; y=yy;
	}
};
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
queue<node> q;
void bfs(int x,int y){
	memset(vis,false,sizeof(vis));
	q.push(node(x,y));
	vis[x][y]=true;
	while(!q.empty()){
		node nd=q.front(); q.pop();
		for(int i=0;i<4;i++){
			int xx=nd.x+dx[i];
			int yy=nd.y+dy[i];
			if(xx<=1||xx>n||yy<=1||yy>m){
				return;
			}
			if(vis[xx][yy]==false&&s[xx][yy]=='0'){
				//cout<<xx<<" "<<yy<<" "<<" "<<"M"<<endl;
				vis[xx][yy]=true;
				q.push(node(xx,yy));
			}
		}
	}
	//cout<<x<<" "<<y<<" x "<<endl;
	cnt++;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>s[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(s[i][j]=='0'){
				bfs(i,j);
			}
		}
	}
	cout<<cnt;
	return 0;
}
2024/6/27 16:41
加载中...