panrong @ 2024-09-28 12:22:41
#include <bits/stdc++.h>
using namespace std;
struct aaa{
int x,y;
};
int n;
int a[110][110]{};
int e[110][110]{};
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
char b;
cin>>b;
if(b=='#'){
a[i][j]=0;
}
else a[i][j]=1;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==1||a[i+1][j]==1||a[i-1][j]==1||a[i][j+1]==1||a[i][j-1]==1){
e[i][j]=1;
}
}
}
//上涨后的地图
queue<queue <aaa>> b;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]==0){
queue <aaa> c;
c.push({i,j});
queue <aaa> d;
while(!c.empty()){
int dx=c.front().x,dy=c.front().y;
if(a[dx][dy]==0){
a[dx][dy]=1;
d.push({c.front().x,c.front().y});
}
dx=c.front().x+1,dy=c.front().y;
if(dx>0&&dx<=n&&dy>0&&dy<=n&&a[dx][dy]==0){
c.push({dx,dy});
}
dx=c.front().x-1,dy=c.front().y;
if(dx>0&&dx<=n&&dy>0&&dy<=n&&a[dx][dy]==0){
c.push({dx,dy});
}
dx=c.front().x,dy=c.front().y+1;
if(dx>0&&dx<=n&&dy>0&&dy<=n&&a[dx][dy]==0){
c.push({dx,dy});
}
dx=c.front().x,dy=c.front().y-1;
if(dx>0&&dx<=n&&dy>0&&dy<=n&&a[dx][dy]==0){
c.push({dx,dy});
}
c.pop();
}
b.push(d);
}
}
}
//每个岛屿分别占用哪些地方
int s=0;
while(!b.empty()){
queue <aaa> c=b.front();
int ddd=0;
while(!c.empty()){
if(e[c.front().x][c.front().y]==0){
ddd=1;//岛屿的这个点还在
break;
}
c.pop();
}
if(!ddd){//如果岛屿每个原有的点都被不存在了,则该岛屿被淹没
s++;
}
b.pop();
}
cout<<s;
return 0;
}
by panrong @ 2024-09-28 12:24:06
错误点:
Wrong Answer.wrong answer On line 1 column 1, read 0, expected 6.
by sunruiqi @ 2024-09-28 12:47:43
大哥,您就不能写一个BFS自定义函数吗?
手搓的BFS读起来都挺累