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. 小程序笔记三:幻灯片swiper 和图片自定义高度

    滑动组件:scroll-view wxml代码 <view> <scroll-view scroll-x="true" class="tab-h&quo ...

  2. 避免使用jQuery的html方法来替换标签,而是使用replaceWith方法

    tblCostSplit.html内容: <nobr title="">只想显示里面内容,去掉nobr标签</nobr> <nobr title=&q ...

  3. Oracle统计每条数据的大小

    怎么查询一条记录到底占了多少空间呢,随便用一个表举例(如上图),就着解决眼前问题的原则(oracle),网上简单查了查,发现生效了,就没深入了解了,包括其它数据库怎么解决,都没做研究.Oracle下, ...

  4. Vue slot简单理解

    情形一: 子组件定义了具名的slot,父组件使用具名的slot,slot显示顺序为子组件定义slot的顺序 子组件: Vue.component('child',{ template:`<div ...

  5. 18 如何使用go来采集windows的基本硬件信息后发送到CMDB的服务器上

    preface 之前我使用python写了cmdb采集的脚本,打包成exe的二进制文件后放在windows上执行,也达到了预期的效果. 但是最近部门要上open-falcon监控体系,每个服务器都要安 ...

  6. srv.exe蠕虫病毒~

    你是否在电脑使用过程中遇到过这样的问题: 1.文件运行后,同目录下会出现一个原名 srv.exe的文件 2.文件运行后会把浏览器打开 3.电脑上的html文件末尾会增加一大堆东西 完了,电脑中了srv ...

  7. CentOS6.8手动安装MySQL5.6

    众所周知,mysql5.7推出后有很多没有填好的坑,对于老的系统和项目兼容性也存在问题,所以现在普遍的web项目还是应该跑在centos6.8+mysql5.6的环境之下,今天主要说一下mysql5. ...

  8. c# 匿名反序列化

    1.先new一个匿名对象,然后再反序列化好处是能点点点,坏处是得先new匿名对象 2.借用Newtonsoft.Json.Linq.JObject.Parse,好处是不需要new匿名对象,坏处是不能点 ...

  9. Java使用选择排序法对数组排序

    编写程序,实现将输入的字符串转换为一维数组,并使用选择排序法对数组进行排序. 思路如下: 点击"生成随机数"按钮,创建Random随机数对象: 使用JTextArea的setTex ...

  10. JAVA WEB ------ 文件下载及导出数据到office Execl表格

    文件下载需要五步: 1.设置文件ContentType类型 // 设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("mu ...