输入样例1:
7
.......
.##....
.##....
....##.
..####.
...###.
.......
输出样例1:
1
输入样例2:
9
.........
.##.##...
.#####...
.##.##...
.........
.##.#....
.#.###...
.#..#....
.........
输出样例2:
1
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n,vis[N][N],k;
char ch[N][N];
int check(int x,int y){
return x>0&&y>0&&x<=n&&y<=n;
}
int dir[4][2]={1,0,0,1,-1,0,0,-1};
void dfs(int x,int y){
for(int i=0;i<4;i++){
int dx=x+dir[i][0];
int dy=y+dir[i][1];
if(check(dx,dy)&&!vis[dx][dy]&&ch[dx][dy]=='#'){
vis[dx][dy]=k;
dfs(dx,dy);
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>ch[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(ch[i][j]=='#'&&!vis[i][j]){
k++;
vis[i][j]=k;
dfs(i,j);
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(ch[i][j]=='#'){
for(int p=0;p<4;p++){
int dx=i+dir[p][0];
int dy=j+dir[p][1];
if(check(dx,dy)&&ch[dx][dy]=='.'){
vis[i][j]=0;
}
}
}
}
}
set<int>res;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(vis[i][j]!=0) res.insert(vis[i][j]);
}
}
cout<<k-res.size();
return 0;
}