POJ 2386 Lake Counting(搜索联通块)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 48370 | Accepted: 23775 |
Description
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* 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
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
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(搜索联通块)的更多相关文章
- POJ 2386 Lake Counting 搜索题解
简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- [POJ 2386] Lake Counting(DFS)
Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
随机推荐
- iOS:PSTCollectionView
https://github.com/steipete/PSTCollectionView Open Source, 100% API compatible replacement of UIColl ...
- php下webservice使用总结
基于thinkphp3.2的 1.修改php配置 php.ini extension=php_soap.dll soap.wsdl_cache_enabled=0 2.soap有两种模式 wsdl和 ...
- 安装kafka集群
1解压tar包 tar -zxvf kafka_2.-.tgz 2.进入config目录 3.配置server.properties文件 # Licensed to the Apache Softwa ...
- pom.xml 配置maven私服
1.pom.xml 配置maven私服 <repositories> <repository> <id>caf_repositories& ...
- linux环境下pytesseract的安装和央行征信中心的登录验证码识别
首先是安装,我参考的是这个 http://blog.csdn.net/xinghun_4/article/details/47860645 我是centos,使用yum yum install pyt ...
- [原]单片机/Stm32教程
1 http://www.amobbs.com/forum.php?mod=viewthread&tid=4462962 2.http://bbs.21ic.com/forum.php?mod ...
- Python--异常处理--12
Python 异常处理 原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ python提供了两个非常重要的功能来处理python程序在运行中出现的 ...
- [AX2012]关于财务默认维度
和以前的版本一样,AX2012中很多地方都使用财务维度,比如客户.销售订单.销售订单行等,根据相应的财务维度设置,生成的相应财务分录将带有财务维度,方便后续对财务分录交易的分析.下图是在客户记录上设置 ...
- win10禁止更新的方法
Windows10强制更新苦恼了很多人,下面提供三种禁止Windows10更新的方法. 禁止Windows update服务 启动任务管理器->选择服务->打开服务. 找到Windows ...
- 消息中间件activemq-5.14.1安全验证配置
activemq分为控制端和客户端,下面分别介绍安全认证配置方法. 1.控制端安全配置 (1). ActiveMQ目录conf下找到jetty.xml: <bean id="secur ...