代码比较简单,挺适合练习深度搜索的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; // 严格要求
char a[1010][1010];
ll ax[9] = {0, -1, 1, 0, 0, -1, 1, -1, 1}; //上下左右 左上左下右上右下
ll ay[9] = {0, 0, 0, 1, -1, -1, -1, 1, 1}; //上下左右 左上左下右上右下
ll n, m, sum = 0;
void dfs(int x, int y){
a[x][y] = '.';
for(ll i = 1; i <= 8; i++){
ll dx = x + ax[i];
ll dy = y + ay[i];
if(dx >= 1 && dx <= n && dy >= 1 && dy <= m && a[dx][dy] == 'W'){
dfs(dx, dy);
}
}
}
int main(){
ios :: sync_with_stdio(0); // 提高cin、cout的运行速度
cin >> n >> m;
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= m; j++){
cin >> a[i][j];
}
}
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= m; j++){
if(a[i][j] == 'W'){ // 每当扫描到一个池塘就调用一次 调用一次就加一次
dfs(i, j);
sum++;
}
}
}
cout << sum << endl;
return 0;
}