Poj2386 Lake Counting (DFS)

Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 49414   Accepted: 24273

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. 

Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M 

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint

OUTPUT DETAILS: 

There are three ponds: one in the upper left, one in the lower left,and one along the right side.
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 using namespace std;
 5 char a[105][105];
 6 int n,m;
 7 void dfs(int x,int y)
 8 {
 9     a[x][y]='.';
10     for(int dx=-1;dx<=1;dx++){
11         for(int dy=-1;dy<=1;dy++){
12             int nx=x+dx,ny=y+dy;
13             if(nx>=0&&nx<n&&ny>=0&&ny<m&&a[nx][ny]=='W'){
14                 dfs(nx,ny);
15             }
16         }
17     }
18     return ;
19 }
20 void solve()
21 {
22     int res=0;
23     for(int i=0;i<n;i++){
24         for(int j=0;j<m;j++){
25             if(a[i][j]=='W'){
26                 dfs(i,j);
27                 res++;
28             }
29         }
30     }
31     cout<<res<<endl;
32 }
33 int main()
34 {
35     while(cin>>n>>m){
36         for(int i=0;i<n;i++){
37             for(int j=0;j<m;j++){
38                 cin>>a[i][j];
39             }
40         } 
41         solve();
42     } 
43     return 0;
44 }

 自己再熟悉一遍:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 #include <string>
 5 #include <cstring>
 6 using namespace std;
 7 int n,m;
 8 char a[105][105];
 9 int res;
10 int nx,ny; 
11 void dfs(int x,int y)
12 {
13     //八个方向搜索 
14     for(int i=-1;i<=1;i++){
15         for(int j=-1;j<=1;j++){
16             nx=x+i,ny=y+j;
17             if(nx>=0&&nx<n&&ny>=0&&ny<m&&a[nx][ny]=='W'){
18                 a[nx][ny]='.';
19                 dfs(nx,ny);
20             }    
21         }
22     }
23 }
24 void solve()
25 {
26     res=0;
27     //每次从发现W的时候开始搜索由于一簇只能算一个地方所以res计数只加一次 
28     for(int i=0;i<n;i++){
29         for(int j=0;j<m;j++){
30             if(a[i][j]=='W'){
31                 res++;
32                 dfs(i,j);
33             }
34         }
35     }
36 }
37 int main()
38 {
39     while(cin>>n>>m){
40         memset(a,0,sizeof(a));
41         for(int i=0;i<n;i++){
42             for(int j=0;j<m;j++){
43                 cin>>a[i][j];
44             }
45         }
46         solve();
47         cout<<res<<endl;
48     }
49     return 0;
50 } 

 

posted @ 2019-03-19 19:39  wydxry  阅读(259)  评论(0编辑  收藏  举报
Live2D