题目原文

描述

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. 如何将第三方jar包上传到Nexus私服

    首先登陆私服服务器         以动力威视私服为例:          自己搭建的私服地址:http://192.168.1.5:8081/nexus/index.html#view-reposi ...

  2. centos7 管理开机启动:systemd

    一.CentOS7 systemd 介绍 在 CentOS7 中,使用 systemd 来管理其他服务是否开机启动,systemctl 是 systemd 服务的命令行工具 [root@mysql ~ ...

  3. 前端 HTML 常用标签 head标签相关内容 title标签 网页的标题信息

    title标签 <title>标签:在<title>和</title>标签之间的文字内容是网页的标题信息,它会显示在浏览器标签页的标题栏中. 可以把它看成是一个网页 ...

  4. 帝国cms用户密码忘记怎么修改

    帝国CMS7.2忘记后台密码怎么找回呢?有时候经常会忘记用户密码,我们可以通过修改数据库的值来初始化密码,下面就和ytkah一起来操作吧 1:进入phpmyadmin 2:找到 phome_enews ...

  5. 张小龙2018PRO版微信公开课演讲全文 透露2018微信全新计划

    大家好!我是张小龙.欢迎大家来到微信公开课. 刚刚出现的是我打游戏的画面,被大家看到了,那个不是我最好的水平,因为有点紧张,我最高分曾打到6000多分.当然我是练习了很久了,并不是我比大家更厉害,而是 ...

  6. Java面试总结(面试流程及核心面试题)

    Java面试流程及核心面试题 面试整体流程 1.1 简单的自我介绍      我是xxxx,工作xxx年.我先后在xxxx公司.yyyy公司工作.先后做个xxxx项目.yyyy项目. 1.2 你简单介 ...

  7. 多线程2.md

    # 多线程  VS 多进程 - 程序:一堆代码以文本形式存入一个文档 - 进程: 程序运行的一个状态 - 包含地址空间.内存.数据栈等 - 每个进程由自己完全独立的运行环境,多进程共享数据是一个问题 ...

  8. python用%来处理字符串

    %s 可以字符串拼接 msg='i am %s my hobby is %s' % ('lhf','alex') print(msg) 执行结果: i am lhf my hobby is alex ...

  9. python的py文件命名注意事项

    最近,在学习python爬虫时,用到各种库特性时,写小段代码,命名demo的py文件诸如:requests.py,json.py,csv.py.都会提示类似“module 'csv' has no a ...

  10. node代码打包为 exe文件---端口进程关闭demo

    最近用到 java,用tomcat起的服务,经常服务关了,对应的进程还在跑,导致再次启动服务失败,需要手动关闭进程. 使用 dos命令虽然只有两行,总是输,也很烦. netstat -ano | fi ...