1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <bits/stdc++.h> using namespace std; const int LEN = 150; int N, M; int vis[LEN][LEN], walk[8][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}}; char arr[LEN][LEN];
void dfs(int x, int y) { if (x < 1 || x > N || y < 1 || y > M) return; if (vis[x][y] || arr[x][y] == '.') return; vis[x][y] = 1; for (int i = 0; i < 8; ++i) dfs(x + walk[i][0], y + walk[i][1]); }
int main() { int ans = 0; cin >> N >> M; for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) cin >> arr[i][j]; for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) { if (arr[i][j] == 'W' && !vis[i][j]) { ++ans; dfs(i, j); } } } cout << ans; return 0; }
|