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. Hart协议

    官方https://fieldcommgroup.org/technologies/hart/documents-and-downloads-hart 参考网页http://www.eeworld.c ...

  2. 牛客网Java刷题知识点之正则表达式(Matcher、Pattern)

    不多说,直接上干货! 正则表达式是什么? 正则表达式是一种可以用于模式匹配和替换的规范, 一个正则表达式就是由普通的字符(例如字符a到z) 以及特殊字符(元字符) 组成的文字模式, 它用以描述在查找文 ...

  3. 编译安装php容易出现的问题以及解决办法

    http://crybit.com/20-common-php-compilation-errors-and-fix-unix/

  4. jar包生成exe可执行程序

    1.生成工具EXE4J下载链接:https://www.ej-technologies.com/download/exe4j/files 2.安装.使用:https://blog.csdn.net/h ...

  5. 解释器模式和php实现

    解释器模式: 给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 角色: 环境角色:定义解释规则的全局信息. 抽象解释器::定义了部分解释具体实现,封装了 ...

  6. ZR#331. 【18 提高 3】括号序列(栈)

    题意 挺神仙的.首先$60$分暴力是比较好打的. 就是枚举左端点,看右端点能否是$0$ 但是这样肯定是过不了的,假如我们只枚举一次,把得到的栈记录下来 那么若区间$(l, r)$是可行的,那么$s_{ ...

  7. Cannot load JDBC driver class 'com.mysql.jdbc.Driver解决方法

    “Cannot load JDBC driver class 'com.mysql.jdbc.Driver ” 表示没有JDBC连接MySql的驱动包,因此需要手动添加驱动包到WEB-INF目录下的l ...

  8. Eclipse IDE配置PHP开发、调试环境

    前言 使用java语言开发的朋友想必对Eclipse开发工具已经不陌生了,那么Eclipse作为java主流的开发工具,是否能够开发PHP项目呢?答案如你所想,肯定是可以的!以下就是该IDE下如何配置 ...

  9. dstat参数选项

    Usage: dstat [-afv] [options..] [delay [count]]Versatile tool for generating system resource statist ...

  10. jsp四大作用域之Session

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...