poj 2386 Lake Counting

Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24578   Accepted: 12407

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.

Source

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 #include<algorithm>
 9 using namespace std;
10 char Map[105][105];
11 int n,m;
12 void dfs(int x,int y){
13     Map[x][y]='.';
14     int i,j;
15     for(i=-1;i<2;i++){
16         int xx=x+i;
17         for(j=-1;j<2;j++){
18             int yy=y+j;
19             if(xx>=0&&yy>=0&&xx<n&&yy<m&&Map[xx][yy]=='W'){
20                 dfs(xx,yy);
21             }
22         }
23     }
24 }
25 int main(){
26     //freopen("D:\\INPUT.txt","r",stdin);
27     scanf("%d %d",&n,&m);
28     int i,j,k;
29     for(i=0;i<n;i++){
30         scanf("%s",Map[i]);
31     }
32     int res=0;
33     for(i=0;i<n;i++){
34         for(j=0;j<m;j++){
35             if(Map[i][j]=='W'){
36                 dfs(i,j);
37                 res++;
38             }
39         }
40     }
41     printf("%d\n",res);
42     return 0;
43 }

 

posted @ 2015-10-04 21:44  Deribs4  阅读(268)  评论(0编辑  收藏  举报