码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2386 - Lake Counting 题解

时间:2017-09-09 09:38:31      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:inpu   方向   正文   ==   output   txt   continue   nbsp   sample   

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

题目链接:http://poj.org/problem?id=2386

题目大意:

给出一张N*M的地图(1<=N,M<=100),地图上的W表示水坑,.表示陆地。水坑是八方向连通的,问图中一共有多少个水坑。

Sample Input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output

6
5

 

分析:

非常简单的深搜。两重循环遍历这个地图,遇到W就dfs来求出这一整个大水坑,并把搜索过的W修改为.

 

AC代码:

 1 #include<algorithm>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<queue>
 5 
 6 inline void read(int &x)
 7 {
 8     char ch = getchar(),c = ch;x = 0;
 9     while(ch < 0 || ch > 9) c = ch,ch = getchar();
10     while(ch <= 9 && ch >= 0) x = (x<<1)+(x<<3)+ch-0,ch = getchar();
11     if(c == -) x = -x;
12 }
13 
14 char mp[110][110];
15 int ans,n,m;
16 
17 void dfs(int x,int y)
18 {
19     if(x == n+1 || y == m+1)
20     {
21         return;
22     }
23     for(int i = -1;i <= 1;++ i)
24         for(int j = -1;j <= 1;++ j)
25         {
26             if(i == 0 && j == 0)
27                 continue;
28             if(x+i<1||x+i>n||y+j<1||y+j>m||mp[x+i][y+j] == .)
29                 continue;
30             mp[x+i][y+j] = .;
31             dfs(x+i,y+j);
32         }
33     return;
34 }
35 
36 int main()
37 {
38 //    freopen("1.txt","r",stdin);
39     read(n),read(m);
40     for(int i = 1;i <= n;++ i)
41         scanf("%s",mp[i]+1);
42     for(int i = 1;i <= n;++ i)
43         for(int j = 1;j <= m;++ j)
44             if(mp[i][j] == W)
45                 ans++,dfs(i,j);
46     printf("%d\n",ans);
47 }

 

POJ 2386 - Lake Counting 题解

标签:inpu   方向   正文   ==   output   txt   continue   nbsp   sample   

原文地址:http://www.cnblogs.com/shingen/p/7497099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!