格外注意行末换行符的处理
#include <iostream>
#include <cstdio>
using namespace std;
int n, m;
char maze[101][101];
int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
void dfs(int x, int y)
{
if (x < 0 || y < 0 || x >= n || y >= m || maze[x][y] == '.')return;
else {
maze[x][y] = '.';
for (int i = 0; i < 8; ++i)dfs(x + dx[i], y + dy[i]);
}
}
int main()
{
int cnt = 0;
scanf("%d%d", &n, &m);
getchar();
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
scanf("%c", &maze[i][j]);
}
getchar();
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
if (maze[i][j] == 'W'){
dfs(i, j);
++cnt;
}
}
}
cout << cnt << endl;
return 0;
}