转载请注明出处:

viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://poj.org/problem?id=1562

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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

代码例如以下:

#include <iostream>
#include <algorithm>
using namespace std;
#include <cstring>
#define TM 100+17
int N, M;
char map[TM][TM];
bool vis[TM][TM];
int xx[8]={0,1,1,1,0,-1,-1,-1};
int yy[8]={1,1,0,-1,-1,-1,0,1};
void DFS(int x, int y)
{
vis[x][y] = true;
for(int i = 0; i < 8; i++)
{
int dx = x+xx[i];
int dy = y+yy[i];
if(dx>=0&&dx<N&&dy>=0&&dy<M&&!vis[dx][dy]&&map[dx][dy] == 'W')
{
vis[dx][dy] = true;
DFS(dx,dy);
}
}
}
int main()
{
int i, j;
while(cin>>N>>M)
{
int count = 0;
memset(vis,false,sizeof(vis));
for(i = 0; i< N; i++)
{
cin>>map[i];
}
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
if(map[i][j] == 'W' && !vis[i][j])
{
count++;
DFS(i,j);
}
}
}
cout<<count<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj2386 Lake Counting(简单DFS)的更多相关文章

  1. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  2. POJ2386 Lake Counting 【DFS】

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20782   Accepted: 10473 D ...

  3. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  4. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  5. POJ:2386 Lake Counting(dfs)

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

  6. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

  7. poj-2386 lake counting(搜索题)

    Time limit1000 ms Memory limit65536 kB Due to recent rains, water has pooled in various places in Fa ...

  8. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  9. POJ 2386——Lake Counting(DFS)

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

随机推荐

  1. hdu4602(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4602 题意:对于每个数的分解,列出其元素的出现的个数. 1    2   3    4    5 1  ...

  2. 30岁生日,媳妇赏的,U-BOAT手表一枚-数字尾巴

    http://bbs.dgtle.com/thread-139611-1-1.html

  3. 别样JAVA学习(五)继承上(1.0)Object类equals()

    上一节继承下(一)我们进行抽象类.接口以及多态的学习. 接下来大家我们讲点特殊的东西就是object类, 我们一直在说继承,子继承了父,父还有没有父类呢, 为什么这么思考,大家想构造函数的第一行是不是 ...

  4. jQuery插件实战之fullcalendar(日历插件)Demo

    jQuery的插件许多,应用的场景也很丰富,今天我这里给大家介绍一款很有用的日历页面开发插件 - fullcalendar,眼下最新版本号是1.5.1,使用这款插件可以高速帮助你高速编程实现基于web ...

  5. apache2.4.4启用deflate压缩

    今天在看<高性能php应用开发>这本书,说道如何启用mod_deflate: 启用如下模块: LoadModule deflate_module modules/mod_deflate.s ...

  6. 用户手册User Guide的写法

    下面的内容仅代表个人观点,是在工作中总结出来的,如果有错误之处,还请指教. 转载请注明来自博客园---”邦邦酱好“: http://www.cnblogs.com/bangbangjiang/p/36 ...

  7. Java的wait(), notify()和notifyAll()使用心得(转)

    本篇文章是对java的 wait(),notify(),notifyAll()进行了详细的分析介绍,需要的朋友参考下wait(),notify()和notifyAll()都是java.lang.Obj ...

  8. android一些面试题目

    1.ListView怎么提高滑动效率 2.说下你做过项目的包的构架,(联网,解析,activity,database) 重点 3.载入大量图片怎么做(包含小图和查看大图) 怎么降低一次跟server的 ...

  9. Oracle SQL Lesson (6) - 使用Join进行联合查询

    使用连接SQL 1999SELECT table1.column, table2.columnFROM table1[NATURAL JOIN table2] |[JOIN table2 USING ...

  10. 解决TabActivity中子页面不通过导航跳转到还有一个页面的问题

    问题:当你的导航在TabActivity中 而子页面的一个button须要切换到当中的某一个导航页面 转载请注明出处:http://blog.csdn.net/x605940745 demo下载地址: ...