zoukankan      html  css  js  c++  java
  • 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
    ",res);
    42     return 0;
    43 }
  • 相关阅读:
    Go控制协裎并发数量的用法及实际中的一个案例
    使用Go处理带证书的请求(含发送POST请求的具体实现)
    利用递归的方式获取restful风格有nextUrl接口返回的数据
    使用Go解析HTTP返回数据为struct并存入数据库的操作
    Windows下安装Redis
    PHP自动加载composer下载的类库
    composer安装第三方类库
    Windows上composer安装
    ThinkPHP5分布式数据库读写分离
    MySQL主从同步及读写分离
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4855058.html
Copyright © 2011-2022 走看看