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. android 静默安装 卸载 资料汇总

    1. android + eclipse + 后台静默安装(一看就会) 2. 适用于android1.5以下版本apk静默安装 3. error: INSTALL_FAILED_SHARED_USER ...

  2. C# WORD操作实现代码(转载)

    在当前项目开发过程中,客户有根据数据库数据生成WORD文档的需求,在和同事沟通的过程中,找到了两个解决方案 1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC. 2.定制W ...

  3. centos 离线安装Ambari

    1.首先要下载它们的安装包,别尝试着在yum来下,特别慢,最好的方式是通过建立本地资源的方式来安装. http://public-repo-1.hortonworks.com/HDP/centos6/ ...

  4. Git项目协同开发学习笔记1:项目库开发基础git命令

    这年头git基本都是项目开发的标配,之前刚好碰到了就花了两天时间系统学习了下.本文内容基本来自以下tutorial:Learn Git(建议直接去看原文,因为这个网站是有更新的).这个是我看过对git ...

  5. workerman定时器使用 php定时任务

    add int \Workerman\Lib\Timer::add(float $time_interval, callable $callback [,$args = array(), bool $ ...

  6. Dubbo -- 系统学习 笔记 -- 示例 -- 服务分组

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 服务分组 当一个接口有多种实现时,可以用group区分. <dubbo:se ...

  7. mysql asyn 实战

    创建configuration时,发现URLParser找不到,于是只能使用配置文件来,当然使用配置文件比使用URL初始化还要直观些 def configurationWithPassword = n ...

  8. 九度 1482:玛雅人的密码(BFS)

    题目描述: 玛雅人有一种密码,如果字符串中出现连续的2012四个数字就能解开密码.给一个长度为N的字符串,(2=<N<=13)该字符串中只含有0,1,2三种数字,问这个字符串要移位几次才能 ...

  9. Window关闭端口的方法(445/135/137/138/139/3389等)

    为防止漏洞被利用,需要采取必要措施,关闭以上端口,以保证系统更加安全. window2003 关闭135端口的方法 要关闭此端口,只需停止DCOM接口服务即达到目的.下面是详细操作过程. 1.打开“组 ...

  10. Create maintenance backup plan in SQL Server 2008 R2 using the wizard

    You will need to identify how you want your maintenance plan to be setup. In this example the mainte ...