nevershow @ 2024-03-16 17:08:52
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int sum,mm;int n;
void dfs(int i,int j){
if(a[i][j-1]!='.' && a[i][j-1] != 'm' && j-1 > 0) dfs(i,j-1);
if(a[i-1][j]!='.' && a[i-1][j] != 'm' && i-1 > 0) dfs(i-1,j);
if(a[i][j+1]!='.' && a[i][j+1] != 'm' && j+1 <= n) dfs(i,j+1);
if(a[i+1][j]!='.' && a[i+1][j] != 'm' && i+1 <= n) dfs(i+1,j);
if(a[i][j]=='#'){
mm++;
return;
} a[i][j]='.';
return;
}
signed main() {
cin>>n;
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(a[i][j-1]=='.'||a[i-1][j]=='.'||a[i+1][j]=='.'||a[i][j+1]=='.'){
a[i][j]='m' ;
}
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if(a[i][j]!='.'){
sum++;
dfs(i,j);
}
}
}
cout<<sum-mm;
return 0;
}
求调