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. C#汉字字母数字正则

    http://novell.me/master-diary/2014-11-15/regular-express-csharp-example.html https://msdn.microsoft. ...

  2. QT读取文本(字符串)最后一行的方法

    QString str; QTextStream ts(&str); str = this->toPlainText(); ts.seek(str.lastIndexOf("- ...

  3. 再谈vim中多窗口的编辑

    参考:http://blog.csdn.net/shuangde800/article/details/11430659 很好 鼠标在各个窗口间循环移动: ctrl+w+(小写的 hjkl), &qu ...

  4. Bash 会清空从父进程继承来的 OLDPWD

    即便 Bash 没有从父进程继承任何的环境变量,Bash 自己也会创建三个环境变量,分别是: $ env -i bash -c export declare -x OLDPWD declare -x ...

  5. Servlet接口五种方法介绍

    Servlet接口定义了5种方法: init() service() destroy() getServletConfig() getServletInfo() init() 在Servlet实例化后 ...

  6. 如何应对ISP乱插广告(案例分析)

    一.广告从何而来? 利益让人铤而走险,从而推动行业“发展”:广告的利益还真不小,xx房产门户网站上一个广告位少则几千,多则几十万:记得在校读书的时候,刚学会做网站,第一想法就是等自己的网站发展成熟有人 ...

  7. 封装自己的Common.js工具库

    Code/** * Created by LT on 2013/6/16. * Common.js * 对原生JS对象的扩展 * Object.Array.String.Date.Ajax.Cooki ...

  8. php代码规范—2

    如何写出好的 PHP 代码? 在本文中,我们将探讨一些良好的编程习惯,这将帮助你避免代码中的缺陷. 1- 编写模块化代码 良好的PHP代码应该是模块化代码.PHP的面向对象的编程功能是一些特别强大的工 ...

  9. PHP mkdir 0777权限问题

    在linux系统中,即使我们使用root帐号去手工执行php命令: mkdir('test', 0777); 结果文件的权限依然为: drwxr-xr-x 2 root root 4096 Jun 1 ...

  10. 为C#自定义控件添加自定义事件

    这里的自定义控件是由普通控件组合而成的. 希望事件响应代码推迟到使用自定义控件的窗体里写. 步骤一:新建一个用户控件,放两个按钮,Tag分别是btn1,btn2. 这两个按钮的共用单击事件处理代码如下 ...