题目原文

描述

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.

输入

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

    输出
  • Line 1: The number of ponds in Farmer John’s field.

    样例输入

    10 12

    W……..WW.

    .WWW…..WWW

    ….WW…WW.

    ………WW.

    ………W..

    ..W……W..

    .W.W…..WW.

    W.W.W…..W.

    .W.W……W.

    ..W…….W.

    样例输出

    3

    提示

    OUTPUT DETAILS:

    There are three ponds: one in the upper left, one in the lower left,and one along the right side.

题目翻译

描述

由于最近下雨,水汇集在农民约翰的领地各处,这是由一个矩形的N x M(1 < = N < = 100;1 < = M < = 100)矩阵。每个矩阵包含水(’ W ‘)或陆地(’ . ‘)。农民约翰想算出有多少池塘形成在他的领域。与一个水池再8个方向连接的被看作是一个池塘。

给定一个农民约翰领地的地图,求出有多少池塘。

输入

第一行:两个空格分隔的整数:N和M

第二行到N + 1行:M每行字符代表一行的农民约翰的领域。每个字符’ W ‘或’。’。字符与字符之间没有空格。

输出

第1行:池塘的数量

实现

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int head=0,tail=1,q,nextx,nexty,n,m,startx,starty,cont;
int a[100005],b[100005],x[8]={0,1,1,1,0,-1,-1,-1},y[8]={1,1,0,-1,-1,-1,0,1},c;
char map[105][105];
bool t[105][105];
bool chek(int qx,int qy)
{
    if(qx<=n-1&&qy<=m-1&&qx>=0&&qy>=0)return 1;
    return 0;
}
void dfs()
{
    memset(b,0,sizeof(b));
    memset(a,0,sizeof(a));
    a[1]=startx;
    b[1]=starty;
    t[startx][starty]=1;
    head=0;tail=1;
    while(head!=tail)
        {
            head++;
            for(int i=0;i<=7;i++)
            {
                nextx=a[head]+x[i];
                nexty=b[head]+y[i];
                if(!t[nextx][nexty]&&map[nextx][nexty]=='W'&&chek(nextx,nexty))
                {
                    t[nextx][nexty]=1;
                    tail++;
                    map[nextx][nexty]='#';
                    a[tail]=nextx;
                    b[tail]=nexty;
                }
            }
        }
}
int main()
{
    c=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",map[i]);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(map[i][j]=='W')
            {startx=i;starty=j;map[i][j]='#';cont++;dfs();}
    printf("%d\n",cont);
}

[openjudge-搜索]Lake Counting(翻译及实现)的更多相关文章

  1. 深度搜索---------Lake counting

    #include<iostream>#include<cstdio>#include<cstdlib>#define maxn 100char ch[maxn][m ...

  2. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  3. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  4. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  5. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  6. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

  7. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  8. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  9. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

随机推荐

  1. scrapy windows下出现importError:No module named 'win32api'

    scrapy windows下出现importError:No module named 'win32api'需安装 pip install pypiwin32

  2. Pytorch安装教程

    一.准备 Window10系统+Ubuntu16.10系统.Anaconda3.5(python3.6) 二.流程 (1)由于墙的问题,用conda安装Pytorch过程中会连接失败,这是因为Anac ...

  3. oracle中字符串与表数据拼接的用法--“||”

    测试过程中,经常需要批量删除或者插入.修改一些表数据或结构,使用手工复制.粘贴其实很麻烦,所以这是我们就可以使用拼接成sql语句的方法来实现操作数据.下面先讲讲oracle中拼接符 || 的用法,如下 ...

  4. linux read()和write

    参考http://www.cnblogs.com/xiehongfeng100/p/4619451.html 1. read总是在接收缓冲区有数据时立即返回,而不是等到给定的read buffer填满 ...

  5. js中属性类型:数据属性与访问器属性

    js中属性类型分为两种:数据属性和访问器属性 在js中,对象都是由名值对构成的,名:就是我们所说的属性名,值就是属性对应的值(基本值.对象.方法). ECMA-262第5版定义了只有内部才用的特性,描 ...

  6. java 网络编(二)UDP的传输

    发送端: package cn.sasa.netDemo1; import java.io.IOException; import java.net.DatagramPacket; import ja ...

  7. expect 自动完成交互式程序神器

    expect 安装 yum -y install expect # Centossudo apt-get install expect # Ubuntu 作用:自动填写交互式程序的值. 原理:通过读取 ...

  8. 简述 cookies 和 session 的区别

    1.cookie 和 session 的区别是:cookie数据保存在客户端,session数据保存在服务器端: 2.两个都可以用来存私密的东西,同样也都有有效期的说法,区别在于session是放在服 ...

  9. matlab中的常用函数

    1.将变量a保存到a.txt,每个值中间用tab隔开 dlmwrite('a.txt',a,'\t'); 2.对比两个矩阵是否完全一样 isequal(a,b); %返回logic=1,则完全相同 3 ...

  10. 微信小程序分享支持自定义封面图

    微信小程序又发布更新了,刚好昨天支付宝也发布小程序,不能让它抢了风头的节奏.微信小程序主要更新如下:“小程序分享支持自定义封面图,公众号及小程序客服可发送小程序卡片.同时,我们还开放了获取发票抬头,指 ...