Zhangky2021 @ 2022-12-15 20:39:15
#include<iostream>
#include<queue>
using namespace std;
char a[1000][1000], b[1000][1000], vis[1000][1000];
int n, s=0;
struct pos{
int x, y;
pos (int ax, int ay){
x=ax, y=ay;
}
};
void bfs(int x, int y){
queue <pos> q;
q.push(pos(x, y));
while(!q.empty()){
pos now=q.front();
q.pop();
int x=now.x, y=now.y;
if(x<1||x>n||y<1||y>n||vis[x][y]||b[x][y]=='.')continue;
vis[x][y]=1;
q.push(pos(x+1, y));
q.push(pos(x, y+1));
q.push(pos(x, y-1));
q.push(pos(x-1, y));
}
}
int main(){
cin>>n;
for(int i=0;i<=n+1;i++){
for(int j=0;j<=n+1;j++){
a[i][j]='#';
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i+1][j]=='.'||a[i][j+1]=='.'||a[i-1][j]=='.'||a[i][j-1]=='.'||a[i][j]=='.'){
b[i][j]='.';
}
else b[i][j]='#';
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(vis[i][j]==0&&b[i][j]=='#'){
bfs(i, j);
s++;
}
}
}
cout<<s;
return 0;
}
by zhicheng @ 2022-12-15 21:23:26
@Zhangky2021 你这求的是没被完全淹没的吧
by Zhangky2021 @ 2022-12-17 11:56:36
@zhicheng 谢大佬,我太蒻了,连橙题的题意都看不懂