Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28966   Accepted: 14505

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

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

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

 
 
 
解析:DFS。从一个'W'开始,每次把'W'连通的部分消掉,经过多次这种操作之后,图中不再有'W',操作的次数就是结果。
 
 
 
#include <cstdio>

int n, m;
char s[105][105]; bool inField(int r, int c)
{
return r >= 0 && r < n && c >= 0 && c < m;
} void dfs(int x, int y)
{
s[x][y] = '.';
for(int i = -1; i <= 1; ++i){
for(int j = -1; j <= 1; ++j){
int tx = x+i, ty = y+j;
if(inField(tx, ty) && s[tx][ty] == 'W')
dfs(tx, ty);
}
}
} int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", s[i]);
int res = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(s[i][j] == 'W'){
++res;
dfs(i, j);
}
}
}
printf("%d\n", res);
return 0;
}

POJ 2386 Lake Counting的更多相关文章

  1. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  2. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  3. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  4. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  5. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  6. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  7. 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). ...

  8. POJ 2386 Lake Counting DFS水水

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

  9. POJ 2386——Lake Counting(DFS)

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

随机推荐

  1. 企业运营对 DevOps 的「傲慢与偏见」

    摘要:出于各种原因,并非所有人都信任 DevOps .有些人觉得 DevOps 只不过给开发者改善产品提供了一个途径而已,还有的人觉得 DevOps 是一堆悦耳的空头支票,甚至有人认为 DevOps ...

  2. 初学tornado之MVC版helloworld

    作者:the5fire | 标签: MVC  tornado  | 发布:2012-08-06 2:41 p.m. 文接上篇,看我一个简单的helloworld,虽然觉得这个框架着实精小,但是实际开发 ...

  3. [SQL Server系] -- 基本概念

    以下是我总结的 SQL Server 数据库中的一些 基本概念,以便模糊时查询, 欢迎补充 1:主键: 概念: 数据表 经常有  一个列 或 列的组合,其值能唯一地标识表中的每一行.这样的一列或多列称 ...

  4. Project Euler 75:Singular integer right triangles

    题目链接 原题: It turns out that 12 cm is the smallest length of wire that can be bent to form an integer ...

  5. python 时间处理(time和datetime介绍)

    python的有关时间的有哪几种呢?今天我们介绍两个:time和datetime time模块提供各种操作时间的函数 datetime模块定义了下面这几个类: datetime.date:表示日期的类 ...

  6. App应用与思考

    我为什么没有加入苹果的iOS APP移动大军?http://blog.csdn.net/Code_GodFather/article/details/7956858 ----------------- ...

  7. JavaScript基础精华01(变量,语法,数据类型)

    JavaScript是一种脚本语言. 脚本,一条条的文字命令.执行时由系统的一个解释器,将其一条条的翻译成机器可识别的指令,然后执行 JavaScript基本组成 1.基本语法(浏览器基本都支持,有统 ...

  8. Linux功能-RPM命令详解

    一.概述 RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序”,用RPM包方式来发布软件变得越来越流行,主要的原因是这种软件发布 ...

  9. BitMask 使用参考

    对于 Java 类应用,内存方面需要注意: 不要占用大量内存,否则可用内存少:触发 GC 或 OutOfMemoryError: 不要频繁创建对象,频繁内存分配,触发 GC. 对于枚举和常量: 使用枚 ...

  10. Django admin site(三)InlineModelAdmin

    InlineModelAdmin class InlineModelAdminclass TabularInlineclass StackedInline 举例,有两个Model: from djan ...