POJ No.2386 Lake Counting
题目链接:http://poj.org/problem?id=2386
分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下我们应该用dfs遍历到尽头,并将这些联通的W全部改掉,1此dfs后与初始的这个W连接的所有W都替换成了'.',因此直到图中不再存在W为止,最后执行dfs的次数即为水洼的个数。
#include <iostream>
using namespace std;
int n,m;
char a[][];
void dfs(int x,int y){
a[x][y]='.';
for(int dx=-;dx<=;dx++){
for(int dy=-;dy<=;dy++){
int nx=x+dx,ny=y+dy;
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='W') dfs(nx,ny);
}
}
return ;
}
int main(){
int count=;
cin>>n>>m;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
for(int i=;i<n;i++){
for(int j=;j<n;j++){//先从任意一个有dfs的地方开始
if(a[i][j]=='W'){
dfs(i,j);
count++;
}
}
}
cout<<count<<endl;
}
POJ No.2386 Lake Counting的更多相关文章
- POJ 之2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20003 Accepted: 10063 D ...
- 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(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- 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: 53301 Accepted: 26062 D ...
- poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)
http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...
随机推荐
- Elasticsearch查询Index以及删除
查询Index信息 GET /bank HTTP/1.1Host: localhost:9200 { "bank": { "aliases": {}, &quo ...
- oracle 之 安装后pl/sql登录报ora-12154
这个问题一开始困扰了很久. 查的资料是复制一小段代码到tnsnames.ora中 SID名 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ...
- 题解——Codeforces Round #508 (Div. 2) T3 (贪心)
贪心的选取最优解 然后相减好 记得要开long long #include <cstdio> #include <algorithm> #include <cstring ...
- 文件IO(2)
Lseek: *************************************************************************** 实验一: ...
- ToString()格式和用法大全,C#实现保留两位小数的方法
C,货币,2.5.ToString("C"),¥2.50.D,十进制数,25.ToString("D5"),00025.E,科学型,25000.ToString ...
- PHP中cookie思维导图
- python-ConfigParser模块【读写配置文件】
对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...
- unity 截图 压缩 处理
/****************************************************** unity屏幕截图,并转换成Base64码* 作者: lyb* 日期:2017年7月25 ...
- 转 class和struct最本质的区别
class和struct最本质的区别是class是引用类型,而struct是值类型,它们在内存中的分配情况有所区别. 什么是class? class(类)是面向对象编程的基本概念,是一种自定义数据结构 ...
- Ubuntu16 源码方式安装postgresql数据库
依赖工具库 注意:默认用户名是postgres,以下命令是Ubuntu操作系统中的命令 make GCC Zlib 安装命令:sudo apt-get install zlib1g-dev注意有些软件 ...