Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34735   Accepted: 17246

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 Input

  1. 10 12
  2. W........WW.
  3. .WWW.....WWW
  4. ....WW...WW.
  5. .........WW.
  6. .........W..
  7. ..W......W..
  8. .W.W.....WW.
  9. W.W.W.....W.
  10. .W.W......W.
  11. ..W.......W.

Sample Output

  1. 3

题解

这道题显然是一道入门搜索题

【涨涨自信2333】

只要八个方向dfs就好

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5.  
  6. int n,m,ans;
  7. int xx[]={,,,,,,-,-,-},
  8. yy[]={,,-,,,-,,,-};
  9. char a[][];
  10. bool map[][],vis[][];
  11.  
  12. void dfs(int a,int b)
  13. {
  14. for(int i=;i<=;++i)
  15. {
  16. int x=xx[i]+a,y=yy[i]+b;
  17. if(map[x][y]&&!vis[x][y])
  18. {
  19. vis[x][y]=;
  20. dfs(x,y);
  21. }
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. scanf("%d%d",&n,&m);
  28. for(int i=;i<=n;++i)
  29. {
  30. scanf("%s",a[i]);
  31. for(int j=;j<m;++j)
  32. if(a[i][j]=='W')map[i][j+]=;
  33. }
  34. for(int i=;i<=n;++i)
  35. for(int j=;j<=m;++j)
  36. if(map[i][j]&&!vis[i][j])
  37. {
  38. vis[i][j]=;
  39. dfs(i,j);
  40. ans++;
  41. }
  42. printf("%d",ans);
  43. }

【dfs】POJ2386湖计数的更多相关文章

  1. 洛谷——P1596 [USACO10OCT]湖计数Lake Counting

    P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...

  2. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  3. CodeForces 711D Directed Roads (DFS判环+计数)

    题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...

  4. 洛谷P1596 [USACO10OCT]湖计数Lake Counting

    https://www.luogu.org/problemnew/show/P1596 连通块水题... 大体思路是找到是水坑的坐标然后就开始不断递归,往八个方向搜,把连在一起的都标记一遍直到找不到为 ...

  5. [USACO10OCT]湖计数Lake Counting 联通块

    题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is repres ...

  6. $P1596 [USACO10OCT]湖计数Lake Counting$

    \(problem\) 其实这题吧\(DFS\)好写一点(大雾 所以就不讲\(DFS\)了 em \(BFS\)的话 主要是 判重. 方向. 队列.(没了吧 至于位置 用两个队列?还是\(pair\) ...

  7. P1596 【[USACO10OCT]湖计数Lake Counting】

    可爱的题面君~~ 个人感觉这题还是很简单的,就是一个完全不加工的找联通块个数 个人解题思路是先读入,然后循环一遍,遇到水就dfs,并把这个w所在的联通块“删除”,并在答案上加一 最后输出答案 具体注释 ...

  8. Luogu P1596 [USACO10OCT]湖计数Lake Counting

    题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is repres ...

  9. poj2386

    湖计数描述由于最近的降雨,水汇集在不同的地方,在农民约翰的领域,这是代表一个长方形的N×M(1μ= 100:1 = M = 100)平方.每一方都包含水(’w')或干燥的土地(“.”).农民约翰想弄清 ...

随机推荐

  1. Python 点滴 IV

    [继承示意图] 类是实例的工厂, OOP就是在树中搜索属性,类事实上就是变量名与函数打成的包 . 每一个class语句会生成一个新的类对象 . 每次类调用时,就会生成一个新的实例对象 . 实例自己主动 ...

  2. 字符串函数---atof()函数具体解释及实现(完整版)

    atof()函数 atof():double atof(const char *str ); 功 能: 把字符串转换成浮点数 str:要转换的字符串. 返回值:每一个函数返回 double 值.此值由 ...

  3. 以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。

    MainWindow.xaml文件 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...

  4. axios遇到的坑

    axios 可以使用一个config.js 配置文件来管理它的请求信息.具体配置不细说,使用如下.   一,使用配置方式 GET方法: let promise = axios.get(url, con ...

  5. 三、spring cloud 服务提供与调用

    如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用. 案例中有三个角色:服务注册中心.服务提供者.服务消费者,eureka单机版启动既可,流程是首先启动注册中心,服务 ...

  6. nginx+redis实现session的共享

    上一篇我们介绍了nginx实现的负载均衡和动静分离,可看这边. 我们在文章的末尾说到,负载均衡需要面临的一个问题是内存数据的同步.例如:我有A,B两台服务器做了负载均衡,当我在A服务器上执行了登录并且 ...

  7. 自学Python2.4-基本数据类型-字典dict(objct)

    Python dict方法总结 一.字典介绍 1.字典概述 ①字典是python中唯一内建的映射类型.又称关联数组或散列②映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关 ...

  8. 【ANT】一个简单的ANT生成文件build.xml

    <?xml version="1.0" ?> <project default="test"> <target name=&quo ...

  9. APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA

    移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...

  10. HTML+DIV+CSS+JSweb前端基础

    HTML+DIV+CSS+JSweb前端基础 1.<html>和</html> 标签限定了文档的开始和结束点. 属性: (1)  dir: 文本的显示方向,默认是从左向右 (2 ...