Lake Counting--poj2386
Lake Counting
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23950 | Accepted: 12099 |
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.
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.
* 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
这个题可以用深搜也可以用广搜,我就是从这个题,明白了两种搜索方式的不同
大家来体会一下吧!
DFS版:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 char map[101][101]; 7 int mov[8][2]={0,1,0,-1,-1,0,1,0,1,1,-1,-1,1,-1,-1,1}; 8 int m,n; 9 bool can(int x ,int y)//判断这个点能不能走 10 { 11 if(x<0||x>m-1||y<0||y>n-1||map[x][y]=='.') 12 return false; 13 return true; 14 } 15 16 void dfs(int x,int y) 17 { 18 int i,xx,yy; 19 for(i=0;i<8;i++) 20 { 21 xx=x+mov[i][0]; 22 yy=y+mov[i][1]; 23 if(can(xx,yy)) 24 { 25 map[xx][yy]='.';//走一个点就标记一下 26 dfs(xx,yy); 27 } 28 } 29 } 30 int main() 31 { 32 int i,j; 33 while(scanf("%d%d",&m,&n)!=EOF) 34 { 35 int sum=0; 36 for(i=0;i<m;i++) 37 scanf("%s",map[i]); 38 for(i=0;i<m;i++) 39 { 40 for(j=0;j<n;j++) 41 { 42 if(map[i][j]=='W') 43 { 44 map[i][j]='.'; 45 dfs(i,j);//每次进入搜索函数就把这个点周围能走的点走完 46 sum++; 47 } 48 } 49 } 50 printf("%d\n",sum); 51 } 52 return 0; 53 }
BFS版:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 char map[101][101]; 7 int m,n; 8 int mov[8][2]={0,1,0,-1,1,0,-1,0,1,1,-1,-1,1,-1,-1,1}; 9 struct node 10 { 11 int a,b; 12 }ta,tb;//定义一个结构体用来存坐标 13 bool can(node x) 14 { 15 if(x.a<0||x.a>m-1||x.b<0||x.b>n-1||map[x.a][x.b]=='.') 16 return false; 17 return true; 18 } 19 20 21 void bfs(int x,int y) 22 { 23 queue<node> q; 24 ta.a=x; 25 ta.b=y; 26 q.push(ta);//入队 27 while(!q.empty())//直到把队列能访问的都访问过 28 { 29 int i; 30 ta=q.front(); 31 q.pop(); 32 for(i=0;i<8;i++) 33 { 34 tb.a=ta.a+mov[i][0]; 35 tb.b=ta.b+mov[i][1]; 36 if(can(tb)) 37 { 38 map[tb.a][tb.b]='.'; 39 q.push(tb);//如果可以访问就入队 40 } 41 } 42 } 43 } 44 45 46 int main() 47 { 48 int i,j; 49 while(scanf("%d%d",&m,&n)!=EOF) 50 { 51 int sum=0; 52 for(i=0;i<m;i++) 53 scanf("%s",map[i]); 54 for(i=0;i<m;i++) 55 for(j=0;j<n;j++) 56 { 57 if(map[i][j]=='W') 58 { 59 map[i][j]='.'; 60 bfs(i,j); 61 sum++; 62 } 63 } 64 printf("%d\n",sum); 65 } 66 return 0; 67 }