#include<bits/stdc++.h>
//#define lowbit(x) x&(-x)
#define N 402
using namespace std;
typedef pair<int,int> PII;
//int bo[N][N];
string g[100002];
int a,b,c,d;
int dx[]= {0,0,1,-1};
int dy[]= {-1,1,0,0};
int main()
{
//ios::sync_with_stdio();
//cin.tie(0);
//cout.tie(0);
cin>>a>>b;
int is,bao;//is表示岛屿
is=bao=0;
for(int i=0;i<a;i++)
cin>>g[i];
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
queue<PII> q;//bfs模板
int p;
if(g[i][j]!='0')
{
p=0;
++is;
q.push({i,j});
if(g[i][j]>='2') p=1;
g[i][j]='0';
while(!q.empty())
{
PII top=q.front();
q.pop();
for(int z=0;z<4;z++)
{
int nx,ny;
nx=top.first+dx[z];
ny=top.second+dy[z];
if(nx>=0&&nx<a&&ny>=0&&ny<b&&g[nx][ny]>='1')//重点先写不越界
{
q.push({nx,ny});
if(g[nx][ny]>='2') p=1;
g[nx][ny]='0';
}
}
}
if(p) ++bao;//寻找带宝藏的岛
}
}
}
cout<<is<<" "<<bao<<endl;
return 0;
}
寻宝图L2-048----BFS
于 2024-04-22 20:32:12 首次发布