zoukankan      html  css  js  c++  java
  • POJ 2386 Lake Counting (简单深搜)

    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
    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

    题意:

      W表示水洼;.表示空地,八个方向连起来的水洼也算是一个,问有多少水洼。

    思路:

      简单的搜索题目,这里用深搜实现。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    char map[1000][1000];//用来存储图的信息
    
    /*标记是否搜索过,本题目没有使用,因为是一次访问,直接将访问过的修改即可*/
    int logo[1000][1000];
    
    int m,n,sum;
    
    /*表示八个方向,四个方向时,将后面四组删掉就可以了*/
    int dir[8][2]= {0,1, 0,-1, 1,0, -1,0, 1,1, 1,-1, -1,1, -1,-1};
    
    void DFS(int x,int y)
    {
        if(x>=0&&y>=0&&x<n&&y<m)//这里是判断是否越界,根据题目要求改写
        {
            if(map[x][y]=='W')//如果符合条件就继续递归。
            {
                map[x][y]='.';//标记为‘.’防止多次访问
                for(int i=0; i<8; i++)//因为八个方向,所以循环八次。
                    DFS(x+dir[i][0],y+dir[i][1]);
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            sum=0;
            memset(logo,0,sizeof(map));
            getchar();
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    cin>>map[i][j];
                }
                getchar();
            }
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                    if(map[i][j]=='W')
                    {
                        DFS(i,j);
                        sum++;//计数
                    }
            }
            cout<<sum<<endl;
        }
    }
  • 相关阅读:
    【Ubuntu】Ubuntu使用root登录
    【Ubuntu】在Ubuntu 12.04 LTS上安装JDK6
    2012/12/18 水曜日 感怀
    【Ubuntu】Ubuntu Java aptget安装配置
    GRUB,分区,menu.ls,(hd0,1)【转载】
    【ExtJS】错误:this.config[...].width为空或不是对象
    【Ubuntu】Ubuntu常用文件操作命令
    Win7 64bit OS 安装64bit JDK后 不能安装Spket IDE
    IE中控制焦点(asp.net)
    mac os里各种启动参数的含义
  • 原文地址:https://www.cnblogs.com/aiguona/p/7269196.html
Copyright © 2011-2022 走看看