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. result源码

    CREATE TABLE `result` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`thetime` CHAR(100) , `category ...

  2. oracle18c linux x86-64 install 杂记

    132 yum install libstdc++-devel 133 yum install compat-libstdc++-33 135 yum install compat-libcap1 1 ...

  3. Unity判断网络是否连接以及判断是否连接WiFi

    由于项目中的核心模块需要用到网络连接,所以需要首先检测用户是否有网络百度了下,有人说通过连接自己的服务器进行测试的,也有人说通过延迟来判断的最后发现原来Unity是提供了网络判断的方法的.Networ ...

  4. go jwt OAuth2.0

    https://blog.csdn.net/wangshubo1989/article/details/77980316 https://blog.csdn.net/wangshubo1989/art ...

  5. python concurrent.futures包使用,捕获异常

    concurrent.futures的ThreadPoolExecutor类暴露的api很好用,threading模块抹油提供官方的线程池.和另外一个第三方threadpool包相比,这个可以非阻塞的 ...

  6. Android 程序打包及签名(转)

    为什么要签名??? 开发Android的人这么多,完全有可能大家都把类名,包名起成了一个同样的名字,这时候如何区分?签名这时候就是起区分作用的. 由于开发商可能通过使用相同的Package Name来 ...

  7. PHP常用的缓存技术汇总

    一.数据缓存 这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接 ...

  8. Nexus5 破解电信关键步骤

    5儿子终于摔坏了,送去保养之后,发现之前已破解的电信3G竟然无效了,心碎!!!!!!!!!!!!!!!!!! 尝试恢复efs --还好有备份,备份万岁!!! 不行!继续尝试恢复!还是不行!再试!... ...

  9. 对象克隆技术Object.clone()

    Java中对象的创建 clone顾名思义就是复制, 在Java语言中, clone方法被对象调用,所以会复制对象. 所谓的复制对象,首先要分配一个和源对象同样大小的空间,在这个空间中创建一个新的对象. ...

  10. actor binary tree lab4

    forward 与 ! (tell) 的差异,举个例子: Main(当前actor): topNode ! Insert(requester, id=1, ele = 2) topNode: root ...