- js24012 的博客
B5025O 面积(area)
- @ 2026-3-18 17:10:03
B5025O 面积(area)
#include<bits/stdc++.h>
using namespace std;
int n=10,cnt=0,a[101][101];
int dx[]{0,1,-1,0,0};
int dy[]{0,0,0,1,-1};
struct node{
int x,y;
};
bool check(int x,int y){
if(a[x][y] != 0)return false;
if(x < 1 || y < 1 || x > n || y > n)return false;
return true;
}
void bfs(int x,int y){
queue<node> q;
q.push((node){x,y});
a[x][y]=2;
while(!q.empty()){
node t=q.front();
int nx=t.x;
int ny=t.y;
q.pop();
for(int i = 1;i <= 4;i++){
int X=nx+dx[i];
int Y=ny+dy[i];
if(check(X,Y)){
// cout << X << " " << Y<< endl;//
q.push((node){X,Y});
a[X][Y] = 2;
}
}
}
}
signed main(){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin >> a[i][j];
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(i == n || j == n || i == 1 || j == 1){
if(a[i][j] == 0){
bfs(i,j);
}
}
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(a[i][j] == 0){
cnt++;
}
}
}
cout << cnt;
return 0;
}
/*
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
*/