zoukankan      html  css  js  c++  java
  • POJ-2386

    Lake Counting
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 36208   Accepted: 17982

    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.

    题意:

    八连通的'W'为一片水洼,问总共有多少片水洼。

    对于每个'W',搜索其八连通区域,将所有找到的'W'都变成'.',直至所有点都变成'.'。

    期间所进行dfs的次数就是水洼的数目。

    AC代码:

     1 #include<bits/stdc++.h>//POJ提交注意更换头文件
     2 using namespace std;
     3 
     4 char mp[110][110];
     5 
     6 int n,m;
     7 
     8 void dfs(int i,int j){
     9     mp[i][j]='.';
    10     for(int dx=-1;dx<=1;dx++){
    11         for(int dy=-1;dy<=1;dy++){
    12             int nx=i+dx,ny=j+dy;
    13             if(0<=nx&&nx<n&&0<=ny&&ny<m&&mp[nx][ny]=='W')
    14             dfs(nx,ny);
    15         }
    16     }
    17     return ;
    18 }
    19 
    20 int main(){
    21     ios::sync_with_stdio(false);
    22     while(cin>>n>>m){
    23         int ans=0;
    24         for(int i=0;i<n;i++){
    25             for(int j=0;j<m;j++){
    26                 cin>>mp[i][j];
    27             }
    28         }
    29         for(int i=0;i<n;i++){
    30             for(int j=0;j<m;j++){
    31                 if(mp[i][j]=='W'){
    32                     dfs(i,j);
    33                     ans++;
    34                 }
    35             }
    36         }
    37         cout<<ans<<endl;
    38     }
    39     return 0;
    40 } 
  • 相关阅读:
    Spring5源码分析之Bean生命周期
    关系图
    Spring5源码分析之AnnotationConfigApplicationContext
    Spring中好玩的注解和接口
    MyBatis使用总结
    设计模式的应用
    C#:网络传输问题
    工具软件:
    Rar安装包
    C#:注册组件 (cmd)
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/7240366.html
Copyright © 2011-2022 走看看