题意:
要求统计总共被淹没了几个岛屿
在一个岛屿中,若总的地块数量等于被淹没地块的数量,则该岛屿被淹没
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int,int> PII;
const int N=1010;
char g[N][N];
int n,yanmo,all;//淹没的数量和一个岛中地的总数量
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
bool st[N][N];//默认值为false
int res;//记录总共被淹没的岛屿数量
void bfs(int x,int y){
queue<PII> q;
q.push({x,y});
all++;
st[x][y]=true;
while(!q.empty()){
auto now=q.front();
q.pop();
bool has_yanmo=false;
for(int i=0;i<4;i++){
int a=now.first+dx[i],b=now.second+dy[i];
if(g[a][b]=='#'&&!st[a][b]){//是地块且还没有被遍历
st[a][b]=true;
all++;
q.push({a,b});
}
else if(g[a][b]=='.'&&has_yanmo==false){//now这块地附近有海洋,且还没有被统计过
yanmo++;//now这块地会被淹没,计数
has_yanmo=true;//标记已经被淹没
}
}
}
}
int main(){
cin>>n;
memset(g,'.',sizeof g);
for(int i=0;i<n;i++) cin>>g[i];
/**/
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(g[i][j]=='#'&&!st[i][j]){
yanmo=0,all=0;
bfs(i,j);
if(yanmo==all) res++;
}
}
}
cout<<res;
return 0;
}