1 int N, M;
2 char field[MAX_N][MAX_N + 1];//园子
3 //现在位置(x, y)
4 void dfs(int x, int y)
5 {
6 //将现在的位置替换为“.”
7 field[x][y] = ‘.‘;
8 //循环遍历移动的8个方向
9 for(int dx = -1; dx <= 1; dx++)
10 {
11 for(int dy = -1; dy <= 1; dy++)
12 {
13 //向x方向移动dx,向y方向移动dy,移动的效果为(nx, ny)
14 int nx = x + dx, ny = y + dy;
15 if(0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == ‘w‘)
16 dfs(nx, ny);
17 }
18 }
19 return;
20 }
21
22 void solve()
23 {
24 int res = 0;
25 for(int i = 0; i < N; i++)
26 {
27 for(int j = 0; j < M; j++)
28 {
29 if(field[i][j] == ‘W‘)
30 {
31 dfs(i, j);
32 res++;
33 }
34 }
35 }
36 cout << res << endl;
37 }