违规用户名Jx9)zIu @ 2024-08-12 20:26:39
#include<bits/stdc++.h>
using namespace std;
int n;
char a[1005][1005];
bool b[1005][1005];
int s;
void dfs(int x,int y){
if(x==0||x==n+1||y==0||y==n+1){
return;
}
if(a[x-1][y]=='#'){
a[x-1][y]=='.';
dfs(x-1,y);
}
if(a[x+1][y]=='#'){
a[x+1][y]=='.';
dfs(x+1,y);
}
if(a[x][y-1]=='#'){
a[x][y-1]=='.';
dfs(x,y-1);
}
if(a[x][y+1]=='#'){
a[x][y+1]=='.';
dfs(x,y+1);
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
if(a[i][j]=='#'){
b[i][j]=1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]=='#'){
bool f=0;
if(b[i-1][j]==0&&b[i-1][j]=='.'){
f=1;
}
if(b[i+1][j]==0&&b[i+1][j]=='.'){
f=1;
}
if(b[i][j-1]==0&&b[i][j-1]=='.'){
f=1;
}
if(b[i][j+1]==0&&b[i][j+1]=='.'){
f=1;
}
if(f){
a[i][j]='.';
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]=='#'){
a[i][j]='.';
s++;
dfs(i+1,j);
dfs(i-1,j);
dfs(i,j+1);
dfs(i,j-1);
}
}
}
cout<<s;
return 0;
}
名字是被jc了
by xw_lemon @ 2024-08-12 21:16:45
思路有问题,应该要走以下几个步骤:
1:判断开始有几个岛(深搜1/广搜1)
2:判断有哪里沉了(深搜2/广搜2),并且沉了的岛要标记成其他字符
3:判断现在有几个岛(深搜1/广搜1)
4:相减,输出
你这份代码不好调,重构吧,加油
by 违规用户名Jx9)zIu @ 2024-08-13 08:36:55
@xw_lemon
...
by xw_lemon @ 2024-08-13 10:00:04
@kkkissb 是真改不了一点
by QICHUNCHEN12345567 @ 2024-08-16 10:06:42
赞同