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

POJ 2386 Lake Counting DFS

时间:2018-06-22 20:35:41      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:count   就是   its   mil   har   bsp   amp   IV   names   

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

思路很简单,就是一个标准的DFS,不断从W开始遍历,每遍历一个W就改成".",然后朝八个方向深搜,要注意边界。如果八个方向都搜完了,一次深搜就结束了。

那么进行几次深搜就意味着有几个连通的区域。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 105;
 4 int N,M;
 5 char field[maxn][maxn];
 6 
 7 void dfs(int x,int y){
 8     field[x][y] = .;
 9     
10     for(int i=-1;i<=1;++i){
11         for(int j=-1;j<=1;++j){
12             int a = x+i, b = y+j;
13             if(a>=0 && a<N && b>=0 && b<M && field[a][b] == W)
14                 dfs(a,b); 
15         }
16     }
17     return ;
18 } 
19 
20 int main(){
21     cin>>N>>M;
22     for(int i=0;i<N;++i){
23         for(int j=0;j<M;j++)
24             cin>>field[i][j];
25     }
26     int ans = 0;
27     for(int i=0;i<N;++i){
28         for(int j=0;j<M;++j){
29             if(field[i][j] == W){
30                 dfs(i,j);
31                 ans++; 
32             }
33         }
34     }
35     cout<<ans;
36     return 0;
37 }

 

POJ 2386 Lake Counting DFS

标签:count   就是   its   mil   har   bsp   amp   IV   names   

原文地址:https://www.cnblogs.com/liluan99/p/9215314.html

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