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. MySQL.. ERROR! The server quit without updating PID file问题解决

    不小心将服务器OS给重启了,再启动数据库的时候,出现了很奇怪的问题 [root@dev run]# service mysql restart ERROR! MySQL server PID file ...

  2. 解决maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e.错误

    POM文件报错maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supp ...

  3. oracle触发器详解(转)

    触发器是许多关系数据库系统都提供的一项技术.在ORACLE系统里,触发器类似过程和函数,都有声明,执行和异常处理过程的PL/SQL块. 8.1 触发器类型 触发器在数据库里以独立的对象存储,它与存储过 ...

  4. linux上应用程序的执行机制

    linux上应用程序的执行机制 执行文件是如何在shell中被"执行"的.本文中尽可能少用一些源码,免得太过于无 聊,主要讲清这个过程,感兴趣的同学可以去查看相应的源码了解更多的信 ...

  5. JDK安装配置问题

    JDK安装过程中会有两个安装提示,一个是jdk的安装,一个是jre的安装

  6. HTML5入门九---Canvas画布

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. EJB--事务管理 .

    在我们对事务的基本概念以及出现的问题和隔离级别有进一步的了解之后,接下来看看EJB是如何进行事务管理. 在EJB中有两种使用事务的方式.第一种方式通过容器管理的事务,叫CMT(Container-Ma ...

  8. 内存泄露了么: Handlers & Inner Classes

    看到一篇关于handler和匿名类关于内存泄露的文章,觉得不错,充分发挥拿来主义,先放这儿看着! From:http://www.androiddesignpatterns.com/2013/01/i ...

  9. WIN32编程杂记(一)

    1.UNREFERENCED_PARAMETER的用处 作用:告诉编译器,已经使用了该变量,不必检测警告! 在VC编译器下,如果您用最高级别进行编译,编译器就会很苛刻地指出您的非常细小的警告.当你生命 ...

  10. 车牌识别LPR系统系列文章汇总

    这里的LPR的的几篇文章是之前项目的一些相关资料的整理,涉及实验室内部的资料就没有放上来,希望能对想了解这方面的同学,有所帮助,那怕了解个大概也好.知道整体的思路就好.当初就是一个人瞎摸索,走了很多的 ...