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

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.

题意:有一个大小为N*M的园子,雨后起了水。八连通的积水都被认为是连接在一起的。请求出园子里总共有多少水洼?(八连通指的是下图中相对W的*部分)
***
*W*
***
解题思路:从任意的W开始,不停把邻接的部分用'.'代替。1次dfs后与初始的这个W连接的所有W就都被替换成了'.',因此知道图中不在出现'W'为止,总共进行dfs的次数就是最后的答案了,8个方向对应8种状态转移,每个格子作为dfs的参数之多被调用一次,所以复杂度为O(8*n*m)=O(n*m)。
附上代码:
 #include<iostream>
#include<cstdio>
using namespace std;
char map[][];
int n,m;
int dir[][]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
//现在位置为(x,y)
void dfs(int x,int y)
{
//将现在所在位置替换为.
map[x][y]='.';
//循环遍历移动的8个方向
for(int i=;i<;i++)
{
int dx=x+dir[i][];
int dy=y+dir[i][];
//判断(dx,dy)内是否又水
if(dx>=&&dx<n&&dy>=&&dy<m&&map[dx][dy]=='W') dfs(dx,dy);
} }
int main()
{
cin>>n>>m;
int res=;
getchar(); //吸收回车符
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scanf("%c",&map[i][j]);
}
getchar();// 将回车符读掉
}
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
//从有水的地方开始dfs
if(map[i][j]=='W')
{
dfs(i,j);
res++;
}
}
}
cout<<res<<endl;
return ;
}
 

poj2386(简单的dfs/bfs)的更多相关文章

  1. POJ 2243 简单搜索 (DFS BFS A*)

    题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...

  2. 浅谈DFS,BFS,IDFS,A*等算法

    搜索是编程的基础,是必须掌握的技能.--王主任 搜索分为盲目搜索和启发搜索 下面列举OI常用的盲目搜索: 1.dijkstra 2.SPFA 3.bfs 4.dfs 5.双向bfs 6.迭代加深搜索( ...

  3. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  4. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  5. 暴力求解——UVA 572(简单的dfs)

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  6. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  7. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. POJ 3256 (简单的DFS)

    //题意是 K N, M; //有K个牛 N个牧场,M条路 ,有向的  //把K个牛放到任意的n个不同牧场中,问所有牛都可以到达的牧场数的总和  //这是一道简单的DFS题 //k 100 //n 1 ...

  9. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

随机推荐

  1. 使用Pandas_UDF快速改造Pandas代码

    1. Pandas_UDF介绍 PySpark和Pandas之间改进性能和互操作性的其核心思想是将Apache Arrow作为序列化格式,以减少PySpark和Pandas之间的开销. Pandas_ ...

  2. ElasticSearch5.5.1插件分类

    ElasticSearch5.5.1插件分类 附官网介绍:https://www.elastic.co/guide/en/elasticsearch/plugins/5.5/intro.html 一. ...

  3. 【JVM.1】java内存区域与内存溢出

    鲁迅曾说过:Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的“高墙”,墙外面的人想进来,墙里面的人想出去. 一.虚拟机内存分布 Java虚拟机在执行Java程序的过程中会把它所管理的内存 ...

  4. Spring 中配置log4j日志功能

    一,添加log4j依赖包 可从官网上下载该依赖包log4j-x.x.xx.jar,下载后 build path,添加依赖包 二,创建 log4j.properties 配置文件 log4j.prope ...

  5. Elasticsearch学习总结 (Centos7下Elasticsearch集群部署记录)

    一.  ElasticSearch简单介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticse ...

  6. linux上启动tomcat远程不能访问

    linux上关闭防火墙同样访问不了,执行iptables -f即可. 你试一试这个“iptables -F”然后再访问,如果能够访问了,那么需要执行“firewall-cmd --add-port=8 ...

  7. 【个人阅读】M1/M2阶段总结

    1.以前博客的链接 http://www.cnblogs.com/zyctsl/p/4028006.html http://www.cnblogs.com/zyctsl/p/4094011.html ...

  8. 《Linux内核设计与实现》读书笔记 18

    第十八章调试 18.1 准备开始 一个bug:大部分bug通常都不是行为可靠而且定义明确的 一个藏匿bug的内核版本:找出bug首先出现的版本 相关内核代码的知识和运气 18.2内核中的bug 可以有 ...

  9. myeclipse快捷方式汇总

    选择你要注释的那一行或多行代码,按Ctrl+/即可,取消注释也是选中之后按Ctrl+/即可. 如果你想使用的快捷键的注释是的话,那么你的快捷键是ctrl+shift+/我以前都是手动注释的,直接打// ...

  10. Quartz中时间表达式的设置-----corn表达式 (转)(http://www.cnblogs.com/GarfieldTom/p/3746290.html)

    Quartz中时间表达式的设置-----corn表达式 (注:这是让我看比较明白的一个博文,但是抱歉,没有找到原作者,如有侵犯,请告知) 时间格式: <!-- s m h d m w(?) y( ...