xibaby @ 2023-12-02 17:45:06
#include<bits/stdc++.h>
using namespace std;
int n,s1,s2,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char a[1001][1001],b[1001][1001];
void dfs1(int x,int y){
a[x][y]='.';
for(int i=0;i<4;i++)
if(a[x+dx[i]][y+dy[i]]!='.'&&x+dx[i]>0&&x+dx[i]<=n&&y+dy[i]&&y+dy[i]<=n)
dfs1(x+dx[i],y+dy[i]);
return;
}
void dfs2(int x,int y){
b[x][y]='.';
for(int i=0;i<4;i++)
if(b[x+dx[i]][y+dy[i]]=='#'&&x+dx[i]>0&&x+dx[i]<=n&&y+dy[i]&&y+dy[i]<=n)
dfs2(x+dx[i],y+dy[i]);
return;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
scanf("%c",&a[i][j]);
b[i][j]=a[i][j];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j]=='#'&&(a[i-1][j]=='.'||a[i+1][j]=='.'||a[i][j-1]=='.'||a[i][j+1]=='.'))
a[i][j]='-';
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(b[i][j]=='#'){s2++; dfs2(i,j);}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j]=='#'){s1++; dfs1(i,j);}
printf("%d",s2-s1);
return 0;
}
by _Rahab_ @ 2023-12-27 23:44:08
你把你的scanf改成cin就能过了