panrong @ 2024-08-14 11:16:44
#include <bits/stdc++.h>
using namespace std;
int n;
int a[1100][1100]{},b[1100][1100]{},c[1100][1100]{};
int s1=0;
void aaa(int x,int y){//同一个岛屿
b[x][y]=1;
int dx=x,dy=y;
dx=x+1,dy=y;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&b[dx][dy]==0){
aaa(dx,dy);
}
dx=x-1,dy=y;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&b[dx][dy]==0){
aaa(dx,dy);
}
dx=x,dy=y+1;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&b[dx][dy]==0){
aaa(dx,dy);
}
dx=x,dy=y-1;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&b[dx][dy]==0){
aaa(dx,dy);
}
return;
}
bool bbb(int x,int y){//海平面上涨后是否还在
if(a[x][y]==0){
return 1;
}
int dx=x,dy=y;
dx=x+1,dy=y;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&c[dx][dy]==0&&b[dx][dy]==0){
c[dx][dy]=1;
return bbb(dx,dy);
}
dx=x-1,dy=y;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&c[dx][dy]==0&&b[dx][dy]==0){
c[dx][dy]=1;
return bbb(dx,dy);
}
dx=x,dy=y+1;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&c[dx][dy]==0&&b[dx][dy]==0){
c[dx][dy]=1;
return bbb(dx,dy);
}
dx=x,dy=y-1;
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&c[dx][dy]==0&&b[dx][dy]==0){
c[dx][dy]=1;
return bbb(dx,dy);
}
return 0;
}
int s2=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
char bbb;
cin>>bbb;
if(bbb=='.'){
b[i][j]=1;
a[i][j]=b[i][j];
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(b[i+1][j]==1||b[i-1][j]==1||b[i][j+1]==1||b[i][j-1]==1){
a[i][j]=1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(b[i][j]==0){
s1++;
if(bbb(i,j)){
s2++;
}
aaa(i,j);
}
}
}
cout<<s1-s2;
return 0;
}
不是要代码,求错误指出