Time limit1000 ms

Memory limit65536 kB

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是水洼,连起来的W算同一个水洼(是九宫格内的连起来)
题解:dfs搜索,搜索不到了就继续,每一个dfs都可以搜到一个水坑,简而言之,总的dfs的次数就是水坑的个数(dfs重新调用的dfs不算)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
#define PI 3.14159265358979323846264338327950 int N,M;
const int MAX_N=;
char field[MAX_N][MAX_N]; void dfs(int x,int y)
{
field[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 && field[nx][ny]=='W')
dfs(nx,ny);
}
}
return ;
} void solve()
{
int res =;
for(int i=;i<N;i++)
{
for(int j=;j<M;j++)
{
if(field[i][j]=='W')
{
dfs(i,j);
res++;
}
}
}
printf("%d\n",res);
} int main()
{
cin>>N>>M;
for(int i=;i<N;i++)
for(int j=;j<M;j++)
cin>>field[i][j];
solve(); }

poj-2386 lake counting(搜索题)的更多相关文章

  1. POJ 2386 Lake Counting 搜索题解

    简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...

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

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

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

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

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

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

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

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

  6. POJ 2386 Lake Counting(深搜)

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

  7. POJ 2386 Lake Counting

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

  8. [POJ 2386] Lake Counting(DFS)

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

  9. POJ:2386 Lake Counting(dfs)

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

  10. POJ 2386 Lake Counting DFS水水

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

随机推荐

  1. python 6 循环

    循环 要计算1+2+3,我们可以直接写表达式: >>> 1 + 2 + 3 6 要计算1+2+3+...+10,勉强也能写出来. 但是,要计算1+2+3+...+10000,直接写表 ...

  2. Yahoo!团队实践分享:网站性能优化的34条黄金守则

    (一)内容 Yahoo!的Exceptional Performance团队为改善Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客并在各种会议上参与探讨.最佳实 ...

  3. 《java学习二》并发编程

    多线程创建方式 1.继承thread类,重写run方法 CreateThread createThread = new CreateThread();     ------createThread  ...

  4. scp 可以在 2个 linux 主机间复制文件

    Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例: ================== Linu ...

  5. HandlerMapping执行过程。。。

    1.web.xml DispatcherServlet 类 寻址 doDispatch() 2.getHandler(requset) 点击,进入 3.HandlerMapping hm=xxxxxx ...

  6. logback整合Logstash

    1.依赖 <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logsta ...

  7. 【Linux】Ubuntu配置zshell&oh-my-zsh

    zshell:https://archive.codeplex.com/?p=shell oh-my-zsh: https://github.com/robbyrussell/oh-my-zsh 终极 ...

  8. HTML5 JSDOM

    1,HTML5 新语义化标签 - nav -- 表示导航 - header -- 表示页眉 -- 头部 - section -- 表示区块 -- 类似于div - main -- 文档主要内容 - a ...

  9. 学JS的书籍

    1.JavaScript DOM 编程艺术 [说明] 这本书最大的特点就是简明易懂,循序渐进,适合初学者,非常容易上手. 计划:三天读完 读书总结:待写 2.Javascript权威指南 特点是权威. ...

  10. 实现memcopy函数

    实现memcopy函数: void * memcpy(void *dest, const void *src, unsigned int count); { if ((src == NULL) || ...