题目链接: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. python中和生成器协程相关yield from之最详最强解释,一看就懂(二)

    一. 从列表中yield  语法形式:yield from <可迭代的对象实例> python中的列表是可迭代的, 如果想构造一个生成器逐一产生list中元素,按之前的yield语法,是在 ...

  2. Nginx负载均衡中后端节点服务器健康检查的操作梳理

    正常情况下,nginx做反向代理,如果后端节点服务器宕掉的话,nginx默认是不能把这台realserver踢出upstream负载集群的,所以还会有请求转发到后端的这台realserver上面,这样 ...

  3. spring cloud bus原理总结

    1.spring cloud bus spring cloud是按照spring的配置对一系列微服务框架的集成,spring cloud bus是其中一个微服务框架,用于实现微服务之间的通信. spr ...

  4. phpcms全站搜索

    这篇博客已经移至http://www.cnblogs.com/nuanai/p/8028562.html中~~~~~~

  5. 小学四则运算APP 第二次冲刺-第二天

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第二次冲刺阶段时间:11.29~12.09 本次发布的判断题功能界面的设置: activity_panduan_set.xml: < ...

  6. PAT L1-027 出租

    https://pintia.cn/problem-sets/994805046380707840/problems/994805107638517760 下面是新浪微博上曾经很火的一张图: 一时间网 ...

  7. Windows samba history

    https://blogs.technet.microsoft.com/josebda/2013/10/02/windows-server-2012-r2-which-version-of-the-s ...

  8. memcache 分布式缓存

    转载地址:http://www.cnblogs.com/phpstudy2015-6/p/6713164.html 作者:那一叶随风 1.memcached分布式简介 memcached虽然称为“分布 ...

  9. Angular $scope和$rootScope

    <!DOCTYPE html><html ng-app='myModule'><head lang="en"> <meta charset ...

  10. python 授权

    1.“包装”意思是一个已经存在的对象进行包装,不管他是数据类型还是一段代码,可以是对一个已经存在的对象增加新的,删除不要的或者修改其他已经存在的功能 2.包装 包括定义一个类,他的实例拥有标准类型的核 ...