AcWing 1233. 全球变暖https://www.acwing.com/file_system/file/content/whole/index/content/4419714/
这洛谷上的题其实是上一道全球变暖问题的基础版,其实都是bfs对于联通块的问题,但是,洛谷这道就是只是在求连通块的数量
原题链接:https://www.luogu.com.cn/problem/P1451
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
bool st[N][N];
char g[N][N];
int n, m;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int cnt, res;
void bfs(int x, int y)
{
queue<PII> q;
q.push({x, y});
st[x][y] = true;
while(q.size())
{
auto t = q.front();
q.pop();
for(int i = 0; i < 4; i ++)
{
int a = t.x + dx[i], b = t.y + dy[i];
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(g[a][b] == '0')
{
continue;
}
if(st[a][b]) continue;
q.push({a,b});
st[a][b] = true;
}
}
cnt ++;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++) cin >> g[i];
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
if(!st[i][j] && g[i][j] != '0')
{
bfs(i, j);
}
cout << cnt;
return 0;
}