You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
946 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, ans;
char g[N][N];
// 方向数组
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
void dfs(int x, int y) {
g[x][y] = '.'; // 这样可以不用使用st数组
for (int i = 0; i < 8; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (g[tx][ty] == 'W') dfs(tx, ty);
}
}
int main() {
cin >> n >> m;
// 使有深度优先搜索时注意下标从1开始这样相当于构建了一个四周为0的墙不会越界
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] == 'W') { // 找到“ W ”.
dfs(i, j);
ans++; // 统计答案
}
printf("%d\n", ans);
return 0;
}