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

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.

Source


【题意】

对于一个图,八个方向代表相邻,求出相邻的(联通)块的个数

 


【分析】

以一个点W为入口将相邻的W 深搜一遍,同时将他改掉,避免重搜


【代码】

#include<cstdio>
using namespace std;
const int N=105;
int n,m,ans,dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{1,-1},{1,1},{-1,1}};
char mp[N][N];
void dfs(int x,int y){
mp[x][y]='.';
for(int i=0;i<8;i++){
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx<1||ny<1||nx>n||ny>m||mp[nx][ny]=='.') continue;
dfs(nx,ny);
}
}
inline void Init(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
}
inline void Solve(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(mp[i][j]=='W'){
dfs(i,j);
ans++;
}
}
}
printf("%d",ans);
}
int main(){
Init();
Solve();
return 0;
}
 

 

 

POJ 2386 Lake Counting(搜索联通块)的更多相关文章

  1. POJ 2386 Lake Counting 搜索题解

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

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

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

  3. POJ:2386 Lake Counting(dfs)

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

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

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

  5. POJ 2386 Lake Counting(深搜)

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

  6. POJ 2386 Lake Counting

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

  7. [POJ 2386] Lake Counting(DFS)

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

  8. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  9. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

随机推荐

  1. Easyradius 1.699更新,增加用户设备绑定、桥接用户管理功能

    最近几天,由于IDC机房设备的问题,导致OA连接3天一天挂2次,真是把我抑闷着,最事一个烦事特别多 好不容易等了一个五一假期,也不得空,把最近一些网友反馈的OA的一些功能及BUG进行修复,主要是以下几 ...

  2. 关于form表单onsubmi提交

    表单允许客户端的用户以标准格式向服务器提交数据.表单的创建者为了收集所需数据,使用了各种控件设计表单如 INPUT 或 SELECT.查看表单的用户只需填充数据并单击提交按钮即可向服务器发送数据.服务 ...

  3. JSTL XML标签库 使用

    推荐博客:http://blog.sina.com.cn/s/blog_4f925fc30101820u.html XML标签库 JSTL提供了操作xml文件的标签库,使用xml标签库可以省去使用Do ...

  4. 《C程序猿从校园到职场》带领大家从校园走向职场

    七夕节刚过.就有好消息传来:本人新书<C程序猿从校园到职场>正式出版并在各大电商平台上发售了! 以下.让我们一起来赞赏一下纸质书的"风採"吧. 本书文件夹 第1章 概述 ...

  5. .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录?

    .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录? 使用VS2010开发应用程序完毕后,在发布应用程序时,常 ...

  6. 关于SpringMVC的文件上传

    关于文件的上传,之前写过2篇文章,基于Struts2框架,下面给出文章链接: <关于Struts2的文件上传>:http://www.cnblogs.com/lichenwei/p/392 ...

  7. sar监控工具详解

    转自http://www.cnblogs.com/Amaranthus/p/3745680.html SAR NAME: SAR报告,收集,保存系统活动信息 语法: sar  [ -A ] [ -b ...

  8. mysql 两台主主复制配置

    A.服务器 [mysqld] # The TCP/IP Port the MySQL Server will listen on port= server-id= #master-host=10.1. ...

  9. python通过标准输入读取内容,读取键盘输入的内容?接收用户输入?

    需求说明: 在交互式脚本中,需要用户手动输入内容,并对内容进行处理.在这里记录下通过 python的内置函数input()读取标注输入的内容.默认的标准输入是键盘. 操作过程: 1.通过input() ...

  10. alsa wav

    wav_parser.h文件: //File : wav_parser.h //Author : Loon <sepnic@gmail.com> #ifndef __WAV_PARSER_ ...