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. leecode-39. Combination Sum

    1.问题描述: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all ...

  2. mysql查看各个表的大小

    information_schema 数据库,在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHEMA : 数据库名 TABLE_NAME:表名 ENGINE:所使用的存储引 ...

  3. SQL函数TIMEDIFF在Java程序中使用报错的问题分析

    需求背景 (读者可略过)司机每天从早到晚都会去到不同的自动售货机上补货,而且补货次数和路线等也是因人而异,补货依据是由系统优化并指派.但是目前系统还无法实施有效指挥和优良的补货策略,司机的补货活动因此 ...

  4. webpack相关插件

    webpack-merge:开发环境和生产环节的webpaak配置文件的配置合并 file-loader:编译写入文件,默认情况下生成文件的文件名是文件名与MD5哈希值的组合 vue-laoder:编 ...

  5. IDEA安装及基本配置

    IDEA基本介绍 IntelliJ IDEA是JetBrains公司开发的一款开发Java的开发工具,简称IDEA,JetBrains公司还有其他几款优秀的开发工具. IDEA是一款收费软件,在财力允 ...

  6. Got error 28 from storage engine的错误处理

    早上例行检查数据库,发现Got error 28 from storage engine这个错误,天那,我的数据.心里哇凉....备份的时间还是很久以前.最近更新了不少,麻烦大了. 好在找到了解决方法 ...

  7. Docker搭建

    环境基于CentOS64位,内核最好3.10. 1.下载安装 docker      dockersudo yum install docker-io (假如内核版本太低的话,会在下载安装Docker ...

  8. 假如m是奇数,且m>=3,证明m(m² -1)能被8整除

    m是奇数,且m>=3 =>m可以用表达式2n-1,n>=2 =>m²-1 = (2n-1)²-1 =>m²-1 = 4n²-4n+1-1 =>m²-1 = 4n²- ...

  9. Selenium3+webdriver学习笔记3(xpath方式元素定位)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...

  10. 命令方式重新签名apk

    1.(每个指令之间要有一个空格) 注:拿到一个apk后,首先删除META-INF. 1.如果你的电脑装的是jdk1.6,就用下面的命令: 打开命令符,首先直接输入: Jarsigner -keysto ...