2026-11-22 10:24:48
发布于:广东
#include <bits/stdc++.h>
using namespace std;
char g[1010][1010];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int n,m;
struct node{
int x;
int y;
};
void bfs(int x,int y){
queue<node> q;
q.push({x,y});
while(q.size()){
node u=q.front();
q.pop();
g[u.x][u.y]='.';
for(int i = 0;i<4;i++){
int nx=u.x+dx[i];
int ny=u.y+dy[i];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(g[nx][ny]=='.')continue;
q.push({nx,ny});
}
}
}
int main(){
cin >> n >> m;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
cin >> g[i][j];
}
}
int ans=0;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
if(g[i][j]=='#'){
bfs(i,j);
ans++;
}
}
}
/*if(ans){
cout << "There are ";
cout << ans;
cout << " ships.";
}else cout << "Bad placement.";
return 0;
}
这里空空如也


















有帮助,赞一个