悬赏关注(出现的WA)
查看原帖
悬赏关注(出现的WA)
1078808
M15388049303楼主2024/3/3 14:11
#include <bits/stdc++.h>
using namespace std;
int x,y,b[505][505],ans=0;
char a[505][505];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct node{
	int nx;
	int ny;
};
void bfs(int sx,int sy){
	queue<node>q;
	q.push({sx,sy});
	b[sx][sy]=1;
	while(q.size()){
		node tmp=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int tx=tmp.nx+dx[i];
			int ty=tmp.ny+dy[i];
			if(tx>=0&&tx<x&&ty>=0&&ty<y&&a[tx][ty]==0){
				b[tx][ty]=1;
				q.push({tx,ty});
			}
		}
	}
} 
int main(){
	cin>>x>>y;
	for(int i=0;i<x;i++){
		for(int j=0;j<y;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<x;i++){
		for(int j=0;j<y;j++){
			if(i==0||i==x-1||j==0||j==y-1){
				if(a[i][j]=='0'){
					bfs(i,j);
				}
			}
		}
	}
	for(int i=0;i<x;i++){
		for(int j=0;j<y;j++){
			if(a[i][j]=='0'&&b[i][j]==0){
				ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}

测评结果

2024/3/3 14:11
加载中...