zoukankan      html  css  js  c++  java
  • poj 2386 Lake Counting

     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <stack>
    #define Max 101
    using namespace std;
    char mp[Max][Max];
    int n,m,vis[Max][Max],c;
    int dir[8][2] = {0,1,1,0,0,-1,-1,0,1,1,1,-1,-1,1,-1,-1};
    void dfs(int x,int y)
    {
        for(int i = 0;i < 8;i ++)
        {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];
            if(tx < 0 || ty < 0 || tx >= n || ty >= m || mp[tx][ty] == '.' || vis[tx][ty])continue;
            vis[tx][ty] = 1;
            dfs(tx,ty);
        }
    }
    int main()
    {
        cin>>n>>m;
        for(int i = 0;i < n;i ++)
            cin>>mp[i];
        for(int i = 0;i < n;i ++)
        {
            for(int j = 0;j < m;j ++)
            {
                if(mp[i][j] == 'W' && vis[i][j] == 0)
                {
                    vis[i][j] = 1;
                    dfs(i,j);
                    c ++;
                }
            }
        }
        cout<<c<<endl;
    }
    View Code
    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

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 42151   Accepted: 20899
  • 相关阅读:
    投票通过,PHP 8 确认引入 Union Types 2.0
    Laravel 菜鸟的晋级之路
    给公司写的composer包开发的规范
    Swoft 源码剖析
    听说PHP的生成器yield处理大量数据杠杠的
    读懂JWT的使用,你就会用PHP如何实现了
    python标准库及其它应用
    python常用算法题
    python迭代器实例
    python生成器实例
  • 原文地址:https://www.cnblogs.com/8023spz/p/9029805.html
Copyright © 2011-2022 走看看