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.
样例输入
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.

来源
USACO 2004 November
 #include "bits/stdc++.h"

 using namespace std ;
const int maxN = ; const int dx[ ] = { , , , , , - , - , - } ;
const int dy[ ] = { , - , , , - , , , - } ; int mp[ maxN ][ maxN ] ; void DFS ( const int xi , const int yi ) {
if ( !mp[ xi ][ yi ] )return ;
mp[ xi ][ yi ] = false ;
for ( int i= ; i< ; ++i ) {
int xx = xi + dx[ i ] ;
int yy = yi + dy[ i ] ;
DFS( xx , yy ) ;
}
} int main ( ) {
int N , M ;
int _cnt = ;
scanf ( "%d%d" , &N , &M ) ;
getchar ( ) ;
for ( int i= ; i<=N ; ++i ) {
for ( int j= ; j<=M ; ++j ) {
if ( getchar ( ) == 'W' ) mp[ i ][ j ] = true ;
}
getchar ( ) ;
} for ( int i= ; i<=N ; ++i ) {
for ( int j= ; j<=M ; ++j ) {
if ( mp[ i ][ j ] ) {
_cnt ++ ;
DFS ( i , j ) ;
}
}
}
cout << _cnt << endl ;
return ;
}

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. 14个技巧助你适配 iOS10

    1.Notification(通知) 自从 Notification 被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到 Us ...

  2. Linux进程间通信(一): 信号 signal()、sigaction()

    一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...

  3. 【转】8G内存下MySQL的优化详细方案

    对于任何一个数据库管理系统来说,内存的分配使用绝对可以算的上是其核心之一了,所以很多希望更为深入了解某数据库管理系统的人,都会希望一窥究竟,我也不例外. 这里给出方案 按照下面的设置试试看: key_ ...

  4. Linux下查看操作系统信息、内存情况及cpu信息:cpu个数、核心数、线程数

    1.查看物理CPU的个数 [root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc ...

  5. js string 转 int Number()

    var numString = '122'; var numberInt = Number(numString); var res = numberInt/2; 结果: res = 61

  6. 2.mongoDB add user in v3.0 问题的解决(Property 'addUser' of object admin is not a func)

    问题:创建mongodb帐户时,出错 > db.addUser('jyu', 'aerohive')  2015-08-05T20:03:02.767+0800 E QUERY    TypeE ...

  7. jquery手写实现单页滚动导航

    效果说明:点击tab导航,页面滑动到下方相应板块.并且当页面通过鼠标滚动下去时,上方的tab也可以自动切换到当前位置的板块上. 代码说明:js中对两个动作分别写,一个是tab点击下滑到相应板块位置:一 ...

  8. 8.7 jquery-dom manipulation

    // 获得设定内容 [text(),html(),val()]; // 获得设定属性 [attr(),removeAttr()]; // 获得设定 css class [addClass,remove ...

  9. libevent在windows平台下通过vs进行编译

    1.vs中新建一个静态库项目 2.配置头文件目录,将./compat../include../WIN32-Code三个目录添加到文件目录中 3.用记事本打开Makefile.nmake文件,可以看到里 ...

  10. mongoTemplate简单用法(增删改查)

    分页时查找数量: public long countSample(String id) { Query query = new Query(); if (StringUtil.hasText(id)) ...