题目链接: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. [JDBC]ORA-01000: 超出打开游标的最大数(ORA-01000: maximum open cursors exceeded)

    问题产生的原因: Java代码在执行conn.createStatement()和conn.prepareStatement()的时候,相当于在数据库中打开了一个cursor.由于oracle对打开的 ...

  2. 分布式系统session一致性的问题

    session的概念 什么是session? 服务器为每个用户创建一个会话,存储用户的相关信息,以便多次请求能够定位到同一个上下文.这样,当用户在应用程序的 Web 页之间跳转时,存储在 Sessio ...

  3. Linux下部署SSH登录时的二次身份验证环境记录(利用Google Authenticator)

    一般来说,使用ssh远程登录服务器,只需要输入账号和密码,显然这种方式不是很安全.为了安全着想,可以使用GoogleAuthenticator(谷歌身份验证器),以便在账号和密码之间再增加一个验证码, ...

  4. 如何利用Android Studio打包React Native APK

    ok!百度出来的东西很杂,所以,这里介绍一种最简单,最合适我们(新手,应该是吧)的APK的打包方式! 当然!这种打包是基于Android Studio的,所以,注意喽!!!! 废话不多说开始吧! 首先 ...

  5. final个人阅读作业

    一.软件工程M1/M2总结 1.M1阶段总结: 我们团队的软件工程开发是按照前后端来分别开发的,我是负责后端的.我们的项目是做一个北航的社团平台,是一个网站.在后端我们使用的是ruby on rail ...

  6. Beta项目展示

    Team C# 一.团队成员简介 杜正远,队长. 博客地址:http://www.cnblogs.com/kevindu/ 崔强,全职PM. 博客地址:http://www.cnblogs.com/m ...

  7. git心得

    使用Git得到了以下体会: github在新的目录下添加新的文件 git init //在相应的目录下添加 git add //添加目录 git commit -m "first commi ...

  8. 第三个sprint冲刺第一阶段

  9. The Contest CodeForces - 813A (思维)

    Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...

  10. Rabbitmq vs. kafka

    https://mp.weixin.qq.com/s/2i_9TWoF3TsJvG6Dj_75vw http://www.cnblogs.com/valor-xh/p/6348009.html htt ...