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.
解题思路:从任意的'W'开始,不停地把邻接的部分用'.'代替。一次DFS后与初始的这个'W'连接的所有'W'就被替换成了'.',因此直到图中不再存在'W'为止,总共进行的DFS的次数就是最终答案。
AC代码:
 #include<iostream>
#include<cstdio>
using namespace std;
const int maxn=;
int n,m,res;char mp[maxn][maxn];
void dfs(int x,int y){
mp[x][y]='.';
for(int dx=-;dx<=;++dx){
for(int dy=-;dy<=;++dy){
int nx=x+dx,ny=y+dy;
if(<=nx && nx<n && <=ny && ny<m && mp[nx][ny]=='W')dfs(nx,ny);//往8个方向寻找'W'的点
}
}
return;
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<n;++i)scanf("%s",mp[i]);
res=;
for(int i=;i<n;++i)
for(int j=;j<m;++j)
if(mp[i][j]=='W'){dfs(i,j);res++;}
printf("%d\n",res);
}
return ;
}

题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)的更多相关文章

  1. [POJ 2386] Lake Counting(DFS)

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

  2. POJ 2386 Lake Counting DFS水水

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

  3. POJ:2386 Lake Counting(dfs)

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

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

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

  5. POJ 2386 Lake Counting (水题,DFS)

    题意:给定一个n*m的矩阵,让你判断有多少个连通块. 析:用DFS搜一下即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...

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

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

  7. POJ 2386 Lake Counting(深搜)

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

  8. POJ 2386 Lake Counting

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

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

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

随机推荐

  1. 【IntelliJ】IntelliJ IDEA的安装破解及使用

    结合两位大牛CV的,写的很全面,仅供自己使用 转载地:http://www.jianshu.com/p/ad3830095fb3 https://www.cnblogs.com/kangjianwei ...

  2. Session&Cookie 的介绍和使用

    Session介绍与使用 1.Session基本介绍 Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序 ...

  3. Spring Boot实现跨域(转)

    一.方法: 服务端设置Respone Header头中Access-Control-Allow-Origin 配合前台使用jsonp 继承WebMvcConfigurerAdapter 添加配置类 二 ...

  4. ArcGIS for Android入门程序之DrawTool2.0

    来自:http://blog.csdn.net/arcgis_mobile/article/details/8084763 GISpace博客<ArcGIS for Android入门程序之Dr ...

  5. 功能超级强大的网络工具nc

    摘自:http://www.linuxso.com/command/nc.html 功能说明:功能强大的网络工具语 法:nc [-hlnruz][-g<网关...>][-G<指向器数 ...

  6. openWrt 安装管理界面luci中文包

    openWrt15安装管理界面luci中文包   如果刚刷的openwrt15没有中文界面,用ssh连接路由后用opkg安装     root@bang-bang-tang:~# opkg insta ...

  7. C# .NET Visual Studio VS2008如何显示行号

    工具-选项,然后勾选"显示所有设置",然后在文本编辑器下面找到所有语言,勾选"行号"即可.  

  8. cocos2d-x 2.2.3 建project

    2.2以后不再使用模板安装了. 打开终端,进入cocos2d-x文件夹下的tools/project-creator,运行命令 ./create_project.py -project [项目名] - ...

  9. Linux操作系统改动PATH的方法

    1. 暂时改动: 使用export.比如#export PATH=$PATH:/etc/apache/bin 2. 针对用户的改动: vi ~/.bash_profile 增加:export PATH ...

  10. spark hadoop 对比 Resilient Distributed Datasets

    hadoop 迭代消耗大 每次迭代启动一个完整的MapReduce作业 spark 首要目标就是避免运算时 过多的网络和磁盘IO开销 Resilient Distributed Datasets ht ...