Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20782   Accepted: 10473

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

Hint

OUTPUT DETAILS: 



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

Source

睡前水一水。

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define maxn 102
  5.  
  6. char G[maxn][maxn];
  7. int n, m;
  8. const int mov[][2] = {0, 1, 0, -1, 1, 0, -1,
  9. 0, 1, -1, -1, 1, 1, 1, -1, -1};
  10.  
  11. void DFS(int x, int y) {
  12. G[x][y] = '.';
  13. int i, j, nx, ny;
  14. for(i = 0; i < 8; ++i) {
  15. nx = x + mov[i][0];
  16. ny = y + mov[i][1];
  17. if(nx >= 0 && nx < n && ny >= 0 && ny < m && G[nx][ny] == 'W')
  18. DFS(nx, ny);
  19. }
  20. }
  21.  
  22. int main() {
  23. int i, j, ret;
  24. while(scanf("%d%d", &n, &m) == 2) {
  25. for(i = 0; i < n; ++i)
  26. scanf("%s", G[i]);
  27. ret = 0;
  28. for(i = 0; i < n; ++i)
  29. for(j = 0; j < m; ++j)
  30. if(G[i][j] == 'W') {
  31. DFS(i, j);
  32. ++ret;
  33. }
  34. printf("%d\n", ret);
  35. }
  36. return 0;
  37. }

POJ2386 Lake Counting 【DFS】的更多相关文章

  1. Openjudge1388 Lake Counting【DFS/Flood Fill】

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

  2. Poj2386 Lake Counting (DFS)

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

  3. CF990G-GCD Counting【dfs】

    正题 题目链接:https://www.luogu.com.cn/problem/CF990G 题目大意 给出一棵有点权的树,对于每个\(k\)求有多条路径的点权\(gcd\)为\(k\) \(1\l ...

  4. POJ 2386 Lake Counting【BFS】

    题意:给出一个矩形,问有多少块连通的W 当找到W的时候,进行广搜,然后将搜过的W变成点,直到不能再搜,进行下一次广搜,最后搜的次数即为水塘的个数 看的PPT里面讲的是种子填充法. 种子填充算法: 从多 ...

  5. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  6. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  7. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

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

  9. 【dfs】POJ2386湖计数

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

随机推荐

  1. python - unittest - 单元测试

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_unittest.py@ide: PyCharm Communi ...

  2. PTA 11-散列4 Hard Version (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version   (30分) Given ...

  3. PHP允许AJAX跨域请求的两种方法

    * 一. 服务端设置 header 头允许AJAX跨域 ** 代码如下: // 允许 ityangs.net 发起的跨域请求 header("Access-Control-Allow-Ori ...

  4. 设计模式(八)组合模式 Composite

    组合模式: 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 组合模式适用于创建复杂的对象,这个对象包含某些个别的对象以及这些对象的组合. 从 ...

  5. python 缺少包

    https://pypi.python.org/pypi/pdfminer/20140328 到这里下载相应的包,再进行安装. tar  –xivf  pybloomfilter-1.0 cd  py ...

  6. java面试题之你了解守护线程吗?它和非守护线程有什么区别

    程序运行完毕,jvm会等待非守护线程完成后关闭,但是jvm不会等待守护线程. 守护线程最典型的的例子是:GC线程

  7. mybatis读取oracle中blob

    controller: byte[] blob = commonService.getPersonImage(bean.getIdCard()); String base64 = new String ...

  8. javaweb之Filter详解

    一.概念:Filter也称之为过滤器,它是Servlet技术中比较激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或 ...

  9. ace模板dataTables_length控制是否显示分页

    默认的ace中配置的是7列之后才显示分页的,其实是可控的,如下: aoColumns这个参数的含义: 排序控制 $(document).ready(function() {$('#example'). ...

  10. 慕课爬虫实战 爬取百度百科Python词条相关1000个页面数据

    http://www.imooc.com/learn/563 spider_main.py #!/usr/bin/python # coding=utf-8 #from baike_spider im ...