AcWing 1233. 全球变暖
原题链接
简单
作者:
a_little
,
2024-03-25 20:26:28
,
所有人可见
,
阅读 1
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
#define x first
#define y second
typedef pair<int,int> PII;
bool st[N][N];
char g[N][N];
int n;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
void bfs(int x,int y,int &total,int &bound){
queue<PII> q;
q.push({x,y});
st[x][y]=true;
while(q.size()){
auto t=q.front();
q.pop();total++;
int fg=0;
for(int i=0;i<4;i++){
int x=t.x+dx[i],y=t.y+dy[i];
if(x<0||x>=n||y<0||y>=n) continue;
if(st[x][y]) continue;
if(g[x][y]=='.') {
fg=1;
continue;
}
q.push({x,y});
st[x][y]=true;
}
if(fg) bound++;
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++) scanf("%s",g[i]);
int cnt=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int total=0,bound=0;
if(g[i][j]=='#'&&!st[i][j]){
bfs(i,j,total,bound);
if(total==bound) cnt++;
}
}
}
cout<<cnt;
return 0;
}