zhz_aleph_114514 @ 2022-11-30 19:04:25
code:
#include<bits/stdc++.h>
#include<queue>
#define ll long long
#define ull unsigned long long
using namespace std;
struct node{
int x,y;
};
char mapp[1005][1005];
char tmp[1005][1005];
int n;
queue<node> q;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void bfs(int x6,int y6){
q.push(node{x6,y6});
mapp[x6][y6]='$';
int kx,ky;
int xx,yy;
while(!q.empty()){
node kkk=q.front();
q.pop();
kx=kkk.x; ky=kkk.y;
for(int i=0;i<4;i++){
xx=kx+dx[i];
yy=ky+dy[i];
if(mapp[xx][yy]=='#'&&xx>=0&&yy>=0&&xx<n&&yy<n){
mapp[xx][yy]='$';
q.push(node{xx,yy});
}
}
}
}
void bfs1(int x6,int y6){
q.push(node{x6,y6});
tmp[x6][y6]='$';
int kx,ky;
int xx,yy;
while(!q.empty()){
node kkk=q.front();
q.pop();
kx=kkk.x; ky=kkk.y;
for(int i=0;i<4;i++){
xx=kx+dx[i];
yy=ky+dy[i];
if(tmp[xx][yy]=='#'&&xx>=0&&yy>=0&&xx<n&&yy<n){
tmp[xx][yy]='$';
q.push(node{xx,yy});
}
}
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>mapp[i][j];
tmp[i][j]=mapp[i][j];
}
}
int t0=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(tmp[i][j]=='#'){
t0++;
bfs1(i,j);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mapp[i][j]=='#'){
if(mapp[i+1][j]=='.'||mapp[i][j+1]=='.'||mapp[i-1][j]=='.'||mapp[i][j-1]=='.'){
mapp[i][j]='$';
}
}
}
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mapp[i][j]=='#'){
ans++;
bfs(i,j);
}
}
}
if(ans>=t0){
cout<<0;
return 0;
}
cout<<t0-ans;
return 0;
}
提交记录
by zhz_aleph_114514 @ 2022-11-30 19:06:11
思路:开始先bfs求出岛屿数量;之后双重for循环模拟淹没岛屿;最后bfs求出未被淹没的岛屿数量,最后相减得出答案
(不确定自己的思路对不对
by jnyz2021109122116 @ 2022-11-30 22:32:41
@zhz_aleph_114514 个人拙见这样判断岛屿数量可能会有bug
如果一个岛屿被完全淹没而另一个岛屿被分割成了两个甚至更多,那么此时被淹没岛屿数量显然至少为1.
by jnyz2021109122116 @ 2022-11-30 22:36:24
比如说这样
7
.......
.##.##.
.#####.
.##.##.
......
...##..
.......