Lake Counting

描述
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.
样例输入
  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.
样例输出
  1. 3
提示
OUTPUT DETAILS:

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

来源
USACO 2004 November
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std ;
  4. const int maxN = ;
  5.  
  6. const int dx[ ] = { , , , , , - , - , - } ;
  7. const int dy[ ] = { , - , , , - , , , - } ;
  8.  
  9. int mp[ maxN ][ maxN ] ;
  10.  
  11. void DFS ( const int xi , const int yi ) {
  12. if ( !mp[ xi ][ yi ] )return ;
  13. mp[ xi ][ yi ] = false ;
  14. for ( int i= ; i< ; ++i ) {
  15. int xx = xi + dx[ i ] ;
  16. int yy = yi + dy[ i ] ;
  17. DFS( xx , yy ) ;
  18. }
  19. }
  20.  
  21. int main ( ) {
  22. int N , M ;
  23. int _cnt = ;
  24. scanf ( "%d%d" , &N , &M ) ;
  25. getchar ( ) ;
  26. for ( int i= ; i<=N ; ++i ) {
  27. for ( int j= ; j<=M ; ++j ) {
  28. if ( getchar ( ) == 'W' ) mp[ i ][ j ] = true ;
  29. }
  30. getchar ( ) ;
  31. }
  32.  
  33. for ( int i= ; i<=N ; ++i ) {
  34. for ( int j= ; j<=M ; ++j ) {
  35. if ( mp[ i ][ j ] ) {
  36. _cnt ++ ;
  37. DFS ( i , j ) ;
  38. }
  39. }
  40. }
  41. cout << _cnt << endl ;
  42. return ;
  43. }

2016-10-19 00:25:45

(完)

POJ 2386 题解的更多相关文章

  1. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  2. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

  3. POJ 2386 Lake Counting 搜索题解

    简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...

  4. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  5. POJ 2386

    http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 #include <stdio.h> #incl ...

  6. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

  7. DFS----Lake Counting (poj 2386)

    Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...

  8. POJ 2386 Lake Counting DFS水水

    http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...

  9. poj 3744 题解

    题目 题意: $ yyf $ 一开始在 $ 1 $ 号节点他要通过一条有 $ n $ 个地雷的道路,每次前进他有 $ p $ 的概率前进一步,有 $ 1-p $ 的概率前进两步,问他不领盒饭的概率. ...

随机推荐

  1. Java学习笔记10

    31.编写当年龄age大于13且小于18时结果为true的布尔表达式age > 13 && age < 18 32.编写当体重weight大于50或身高大于160时结果为t ...

  2. XMPP开发环境配置

    首先配置XMPP开发环境配置需要的软件 先安装xampp-osx-1.8.3-5-installer.dmg 安装成功后launchpad里会多出一个XAMPP(其他),点开里面的manager-os ...

  3. ActiveMQ的静态网络链接

    -------------------------------------------------------------------- (1)ActiveMQ的networkConnector是什么 ...

  4. tyvj1114 搭建双塔

    描述     2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔.    Mr. F有 ...

  5. PHP中global与$GLOBALS['']的区别

    +++ 探讨(一)+++++++++++++++++++++++++++++++++++++++ 很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GL ...

  6. 最大堆 最小堆 解决TOPK问题

    堆:实质是一颗完全二叉树,最大堆的特点:父节点值均大于子节点:最小堆的父节点值均小于子节点: 一般使用连续内存存储堆内的值,因而可以根据当前节点的索引值推断子节点的索引值: 节点i的父节点为(i-1) ...

  7. Java Native Interfce三在JNI中使用Java类的普通方法与变量

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 前面我们学习了如何在JNI中通过参数来使用J ...

  8. T-SQL 语句的理解

    1.T-SQL中各子句在逻辑上按照以下顺序进行处理 . . . .. .ORDER BY 查询实例: SELECT EMPID, YEAR(ORDERDATE) AS ORDERYEAR, COUNT ...

  9. AdminLTE 2 开源模版

    AdminLTE  2 开源模版: 1. 文档  https://almsaeedstudio.com/themes/AdminLTE/documentation/index.html 2. 代码   ...

  10. java20

    1:递归(理解) (1)方法定义中调用方法本身的现象 举例:老和尚给小和尚讲故事,我们学编程 (2)递归的注意事项: A:要有出口,否则就是死递归 B:次数不能过多,否则内存溢出 C:构造方法不能递归 ...