1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include<bits/stdc++.h> using namespace std;
const int N=1e3+10; typedef pair<int,int> PII; int n; char g[N][N]; bool st[N][N]; int dx[4]={1,0,-1,0}; int dy[4]={0,-1,0,1};
int bfs(int x,int y,int &total,int &bound) { queue<PII> q; q.push({x,y}); st[x][y]=true; while(q.size()) { PII t=q.front(); q.pop(); bool is_water=false; total++; for(int i=0;i<4;i++) { int nx=t.first+dx[i]; int ny=t.second+dy[i];
if(st[nx][ny]){ continue; }
if(g[nx][ny]=='.') { is_water=true; continue; } q.push({nx,ny}); st[nx][ny]=true; } if(is_water) {
bound++; }
}
}
int main() { cin>>n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>g[i][j];
int ans=0;
for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(!st[i][j]&&g[i][j]=='#') { int start=0; int boud=0; bfs(i,j,start,boud); if(start==boud) ans++;
}
}
cout<<ans;
return 0; }
|