zoukankan      html  css  js  c++  java
  • Lake Counting(深搜,DFS)

    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.

    #include<iostream>
    #include <deque>
    #include<stack>
    #include<queue>
    #include<math.h>
    #include<algorithm>
    #include<stdlib.h>
    #include<string>
    #include<stdio.h>
    #include<string.h>
    typedef long long LL;
    #define $ 3.141592654
    using namespace std;
    char a[100][100];
    int v[100][100]={0};
    int m,n;
    char f(int x,int y)//判断是否越界
    {
        if(x>=0&&x<m&&y>=0&&y<n)
            return a[x][y];
        return '.';
    }
    void DFS(int x,int y)
    {
         if(v[x][y]==0)
         {
             v[x][y]=1;
             if(f(x-1,y-1)=='W')
                DFS(x-1,y-1);
             if(f(x-1,y)=='W')
                DFS(x-1,y);
             if(f(x-1,y+1)=='W')
                DFS(x-1,y+1);
             if(f(x,y-1)=='W')
                DFS(x,y-1);
             if(f(x,y+1)=='W')
                DFS(x,y+1);
             if(f(x+1,y-1)=='W')
                DFS(x+1,y-1);
             if(f(x+1,y)=='W')
                DFS(x+1,y);
             if(f(x+1,y+1)=='W')
                DFS(x+1,y+1);
         }
    }
    int main()
    {
        int i,j,k,ans=0;
        scanf("%d%d",&m,&n);
        getchar();
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
              scanf("%c",&a[i][j]);
            getchar();
        }
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(v[i][j]==0&&a[i][j]=='W')
                {
                    ans++;

                    DFS(i,j);//当此次调用函数完成时,与该‘W’相邻的所有‘W’都已经被标记了;遍历的时候就不会再满足“v[i][j]==0“这个条件

                }
            }
        }
        printf("%d ",ans);
        return 0;
    }








  • 相关阅读:
    [转] ORACLE 错误编号表一
    基于CkEditor实现.net在线开发之路(1)
    跨行清算系统的实现原理
    应用程序域
    支付机构客户备付金存管办法
    数据库培训二期试题
    MYSQL开发规范
    详解线上线下收单业务(一)第三方支付
    Solr安装配置说明
    进程(Process)
  • 原文地址:https://www.cnblogs.com/jk17211764/p/9677415.html
Copyright © 2011-2022 走看看