- js24012 的博客
O1479 【基础】数池塘(四方向)
- @ 2026-3-18 16:57:26
O1479 【基础】数池塘(四方向)
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
char a[105][105];
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};
bool check(int x,int y){
if(x<1||x>n)return 0;
if(y<1||y>m)return 0;
if(a[x][y]=='.')return 0;
return 1;
}
void bfs(int sx,int sy){
queue<pair<int,int> >q;
q.push({sx,sy});
while(!q.empty()){
pair<int ,int>t=q.front();
int x=t.first,y=t.second;
q.pop();
for(int i = 1;i <= 4;i++){
int X=dx[i]+x,Y=dy[i]+y;
if(check(X,Y)){
a[X][Y] = '.';
q.push({X,Y});
}
}
}
}
signed main(){
cin >> n>>m;
for(int i = 1; i <= n; i++){
for (int j = 1;j<= m; j++){
cin >> a[i][j];
}
}
int cnt=0;
for(int i = 1; i<=n;i++){
for (int j = 1;j<= m; j++){
if(a[i][j] == 'W'){
cnt++;
// cout << i<<" "<<j<<endl;
bfs(i,j);
}
}
}
cout << cnt;
return 0;
}