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. 【周期性执行事件】MySQL事件(Event)&任务调度

    1.事件简介 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”. 事件和触发器类似,都是在 ...

  2. nyoj_176_队花的烦恼二_201404262008

    队花的烦恼二 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 ACM队队花C小+最近在X大OJ上做题,竟发现了一道做不出来的…水题!她快郁闷死了……也许是最近状态不太 ...

  3. MyBatis 3在Insert之后返回主键

    XML: <insert id="addUser" parameterType="User" useGeneratedKeys="true&qu ...

  4. ETL增量单表同步简述_根据dateTime增量

    ETL增量单表同步简述 1. 实现需求 当原数据库的表有新增.更新.删除操作时,将改动数据同步到目标库对应的数据表. 2. 设计思路 设计总体流程图如下: 步骤简单说明: 1.设置job的执行属性,如 ...

  5. Android GIS开发系列-- 入门季(8) Json与Geometry的相互转换

    在Android中json数据十分普遍,也很实用,在Arcgis中也同样支持Json数据,Json与Geometry可以相互转换,达到我们想要的数据. 一.Geometry转换成Json数据 这个实现 ...

  6. 如何使用TFTP客户端工具修复路由器固件

    如何使用TFTP客户端工具修复路由器固件 编号:12083       来自:NetGear       更新日期:2013-10-14       访问数量:24650 NETGEAR无线路由器中, ...

  7. excel2010英文大写怎么变小写

    excel大写转小写步骤如下: 1.如下图所示,若要对字母进行大.小写转换可以使用如下的步骤. 2.若要将单元格中的所有字母全部转换为大写形式,可以使用UPPER函数. 公式为 =UPPER(A2) ...

  8. hdu 2544 最短路(SPFA算法)

    本题链接:点击打开链接 本题大意: 首先输入一个n,m.代表有n个点.m条边.然后输入m条边,每条边输入两个点及边权.1为起点,n为终点.输入两个零表示结束. 解题思路: 本题能够使用SPFA算法来做 ...

  9. Windows 老是弹出要自动拨号连接怎么办

    如下图所示,无论点击取消还是点击关闭按钮都会自动再弹出 点击工具-Internet选项 连接-局域网设置,取消勾选这些东西 点击确定和应用,可能还是会弹出几次拨号的窗口,多点击几次取消,过几下就不弹了 ...

  10. CSS 全部的列表样式类型

    <html> <head> <style type="text/css"> ul.none {list-style-type: none} ul ...