题目原文

描述

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. Selenium功能自动化测试工具

    Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozilla Suite ...

  2. sqlserver2014两台不同服务器上数据库同步

    sqlserver2014两台不同服务器上数据库同步   同步了快一个月了,哈哈,因为途中比较麻烦,第一次,遇到烦的地方就停下了,今天终于同步成功了,哈哈,下面我就来介绍一下我实现两台数据库同步的过程 ...

  3. java 数组(一)

    java的数组操作和C#是一样的,不多说明. public class ArrayDemo{ public static void main(String[] args){ //数组的定义 数据类型[ ...

  4. MySQL 5.7 传统复制到GTID在线切换

    来源:http://wubx.net/ 联系方式: wubingxi#163.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究. 前题: 要求MySQL 5.7.6及以后版本. 所有组 ...

  5. JS中函数表达式与函数声明的区别

    hello,沐晴又来更新啦,今天呢,跟大家讲讲让人头疼的函数表达式和函数声明,反正我当初看那本高级程序的时候,是没怎么看太透,哈哈.我是个比较重基础的人,跟我一起探讨函数表达式和函数声明的世界吧. 首 ...

  6. Number (int float bool complex)--》int 整型、二进制整型、八进制整型、十六进制整型

    # ### Number (int float bool complex) # (1) int 整型 (正整数 0 负整数) intvar = 15 print(intvar) intvar = 0 ...

  7. MySQL深入理解

    [存储引擎] InnoDB表引擎 默认事务型引擎,最重要最广泛的存储引擎,性能非常优秀. 数据存储在共享表空间,可以通过配置分开. 对主键查询的性能高于其他类型的存储引擎. 内部做了很多优化,从磁盘读 ...

  8. api-gateway-engine知识点(2)

    GroupVersion实现engine本地缓存 package com.inspur.cloud.apigw.engine.cache; import java.util.Map;import ja ...

  9. python os.path模块常用方法详解

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...

  10. cocos2d JS-(JavaScript) 几种循环遍历对象的比较

    通常我们会用循环的方式来遍历数组.但是循环是 导致js 性能问题的原因之一.一般我们会采用下几种方式来进行数组的遍历: 方式1: for in 循环: var arr = [1,2,3,4,5]; v ...