题目原文

描述

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. java web如何获取客户端的请求ip

    String ipAddress = request.getHeader("X-FORWARDED-FOR");if (ipAddress == null) { ipAddress ...

  2. 第 7 章 Data 类型

    目录 第 7 章 Data 类型 一.创建方式 二.转时间戳 其他 第 7 章 Data 类型 @(es5) 参考了: 阮一峰javascript的标准.<javascript高级教程> ...

  3. 【托业】【新托业TOEIC新题型真题】学习笔记2-题库一-->P5-6

    P5-6 --------------------------------------单词-------------------------------------- transfrom 转化 jus ...

  4. cors与jsonp

    在.net中,可以在webApiConfig代码里写,也可以在web.config里配置,但都需要引入System.Web.Cors.这些都是服务器端的配置,对整个项目有效. {若只想对某个请求有效, ...

  5. dedecms批量删除文档关键词可以吗

    这几天在重新整服务器,几个站点都是用dedecms搭建的,版本相对比较早,虽然都已经打了补丁,但客户还是在纠结,所以就下载了新的系统进行搭建(注意编码要和原来的一样),导入数据,一切安好,可发现后台有 ...

  6. vc让界面保持最上层

    vc让界面保持最上层.事实上就一个函数就ok了, ::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_TOPMOST,-1,-1,-1,-1,0);

  7. awk命令的基本使用

    命令主要用法 -格式1:前置命令 | awk [选项] '[条件]{编辑指令}' -格式2:awk [选项] '[条件]{编辑指令}' filename 常用命令选项 -F:指定分隔符,可省略(默认空 ...

  8. C#实体对象出现中文处理乱码的问题

    问题: C#实体对象使用时,对于是中文的增加时,到数据库后变成了?? 解决方法: 增加实体对象时“新建连接”操作中的高级中要设置属性Character Set=urf8  (要手动输入)

  9. IE8.0如何关闭启用内存保护帮助减少联机攻击?

    默认情况下,该选项卡是灰色的(表示用户不能直接修改),win7电脑可以通过“以管理员身份”运行 IE 来放开该功能,但个别电脑即便用这种方法仍无法解决,此时,您可以试试如下方法: 1.在“运行”框中输 ...

  10. [js] Array.slice和类数组转数组

    a.call(b) 相当于把a方法放到b的原型上(实例私有方法)执行 Array.slice的用途 https://juejin.im/post/5b20b8596fb9a01e8d6a47c0 用法 ...