1249:Lake Counting
题目分析:
1.八连通积水是连通在一起,叫水洼
2. 求水洼个数:转换为求W连通块个数
代码如下:
#include<iostream>
#include<queue>
using namespace std;
#define x first
#define y second
#define pii pair<int,int>
const int N = 115;
int n,m;
char g[N][N];
bool st[N][N];
void bfs(int sx,int sy){
queue<pii> q;
q.push({sx,sy});
st[sx][sy] = true;
while(q.size()){
pii t = q.front();
q.pop();
for(int i = t.x - 1; i <= t.x + 1; i ++ )
for(int j = t.y - 1; j <= t.y + 1; j ++ )
{
if(i == t.x && j == t.y) continue;
if(i < 0 || i > n - 1 || j < 0 || j > m - 1) continue;
if(st[i][j] || g[i][j] != 'W') continue;
q.push({i,j});
st[i][j] = true;
}
}
}
int main(){
cin >> n >> m;
for(int i = 0; i < n; i ++ ) cin >> g[i];
int cnt = 0;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m; j ++ )
if(!st[i][j] && g[i][j] == 'W') bfs(i,j),cnt ++;
cout<<cnt;
return 0;
}


京公网安备 11010502036488号