POJ 2386 题解
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 题解的更多相关文章
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
- POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...
- POJ 2386 Lake Counting 搜索题解
简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- POJ 2386
http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 #include <stdio.h> #incl ...
- 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). ...
- DFS----Lake Counting (poj 2386)
Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...
- POJ 2386 Lake Counting DFS水水
http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...
- poj 3744 题解
题目 题意: $ yyf $ 一开始在 $ 1 $ 号节点他要通过一条有 $ n $ 个地雷的道路,每次前进他有 $ p $ 的概率前进一步,有 $ 1-p $ 的概率前进两步,问他不领盒饭的概率. ...
随机推荐
- mysql binlog日志优化及思路
在数据库安装完毕,对于binlog日志参数设置,有一些参数的调整,来满足业务需求或使性能最大化.Mysql日志主要对io性能产生影响,本次主要关注binlog 日志. 查一下二进制日志相关的参数 ...
- php5 数据类型
一.PHP主要有一下几种数据类型 String(字符串), Integer(整型), Float(浮点型), Boolean(布尔型), Array(数组), Object(对象), NULL(空值) ...
- tyvj1294 小v的舞会
背景 "梦中伊人,断我男儿几寸柔肠,于断桥,不知西风自憔悴那姑娘."小v的梦中伊人要带领一大帮姐妹MM们来小v家举办舞会,然而怎么安排跳舞的顺序成了大问题,你能帮他么? 描述 有n ...
- R语言:用简单的文本处理方法优化我们的读书体验
博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html 前言 延续之前的用R语言读琅琊榜小说,继续讲一下利用R语言做一些简单的文本处理.分词的事情.其实 ...
- Access应用笔记<四>-一个完整的自动化报表搭建过程
距离之前的三篇日志已经很久啦,今天终于完成了一个比较完整的自动化报表搭建过程 基于公司数据保密原则,样板就不放到网上来了,简单说一下背景: 这次access实现的功能包括: 1)为部门整体搭建了一个员 ...
- Linux C 堆内存管理函数malloc()、calloc()、realloc()、free()详解
C 编程中,经常需要操作的内存可分为下面几个类别: 堆栈区(stack):由编译器自动分配与释放,存放函数的参数值,局部变量,临时变量等等,它们获取的方式都是由编译器自动执行的 堆区(heap):一般 ...
- git实践
撤销提交 根据–soft –mixed –hard,会对working tree和index和HEAD进行重置: git reset –mixed:此为默认方式,不带任何参数的git reset,即时 ...
- winform窗体弹出时,光标默认显示在指定的输入框内
private void Form1_Paint(object sender, PaintEventArgs e) { this.textBox1.SelectAll(); this.textBox1 ...
- sscanf提取字符串中的数据php
1.需求 理解sscanf的作用 2.例子 $str = "age:30 weight:60kg"; sscanf($str,"age:%d weight:%dkg&qu ...
- Qt界面中嵌入其他exe程序的界面,使用Qt5
下面用一个小例子来演示如何在Qt的界面中嵌入其他exe程序的界面,最终效果如下图所示.本文参考了 http://blog.csdn.net/jiaoyaziyang/article/details/4 ...