【DFS】POJ-2386 Lake Counting

题意

给一个n*m的园子,*表示无积水,W表示有积水,求有几块积水

思路

八连通问题,用DFS求解

总结

在紫书上有一道类似的题,之前做过

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <stack>
 6 #include <algorithm>
 7 const int maxn = 100 + 5;
 8 using namespace std;
 9 int n, m;
10 char G[maxn][maxn];
11 const int dx[] = {-1, 0, 1};
12 const int dy[] = {-1, 0, 1};
13 bool isinside(int x, int y)
14 {
15     if(x >= 0 && x < n && y >= 0 && y < m)
16         return true;
17     return false;
18 }
19 void dfs(int x, int y)
20 {
21     G[x][y] = '.';
22     for(int i = 0; i < 3; i++) {
23         int xx = x+dx[i];
24         for(int j = 0; j < 3; j++) {
25             int yy = y+dy[j];
26             if(isinside(xx, yy) && G[xx][yy] == 'W')
27                 dfs(xx, yy);
28         }
29     }
30 }
31 int main()
32 {
33    
34     scanf("%d %d", &n, &m);
35     for(int i = 0; i < n; i++) {
36         scanf("%s", G[i]);
37     }
38     int ans = 0;
39     for(int i = 0; i < n; i++) {
40         for(int j = 0; j < m; j++) {
41             if(G[i][j] == 'W') {
42                 dfs(i, j);
43                 ans++;
44             }
45         }
46     }
47     printf("%d\n", ans);
48     return 0;
49 }

 

posted @ 2017-01-23 17:29  kikii233  阅读(112)  评论(0)    收藏  举报