POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386
《挑战程序设计竞赛》习题
题目描述
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 W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
Sample Output
算法1
将相同的水坑算在一起 并查集
C++ 代码
#include <iostream>
#include <set> using namespace std; #define MAX_NUM 110 int N, M;
char field[MAX_NUM+][MAX_NUM + ];
int fa[MAX_NUM*MAX_NUM]; //char field[10][12] = {
// {'W','.','.','.','.','.','.','.','.','W','W','.'},
// {'.','W','W','W','.','.','.','.','.','W','W','W'},
// {'.','.','.','.','W','W','.','.','.','W','W','.'},
// {'.','.','.','.','.','.','.','.','.','W','W','.'},
// {'.','.','.','.','.','.','.','.','.','W','.','.'},
// {'.','.','W','.','.','.','.','.','.','W','.','.'},
// {'.','W','.','W','.','.','.','.','.','W','W','.'},
// {'W','.','W','.','W','.','.','.','.','.','W','.'},
// {'.','W','.','W','.','.','.','.','.','.','W','.'},
// {'.','.','W','.','.','.','.','.','.','.','W','.'}
//}; //===============================================
// union find
void init(int n)
{
for(int i=;i<=n;i++)
fa[i]=i;
}
int get(int x)
{
return fa[x]==x?x:fa[x]=get(fa[x]);//路径压缩,防止链式结构
}
void merge(int x,int y)
{
fa[get(x)]=get(y);
}
//=========================================================== void Check(int x,int y)
{
//上
int xcopy = x - ;
if (xcopy >= && x < N) {
for (int add = -; add <= ; add++) {
int ycopy = y + add;
if (ycopy >= && ycopy < M && field[xcopy][ycopy] == 'W') {
int idx = x * M + y;
int anotherIdx = xcopy * M + ycopy;
merge(idx, anotherIdx);
}
}
} //中
xcopy = x;
if (xcopy >= && x < N) {
for (int add = -; add <= ; add++) {
if (add == ) continue;
int ycopy = y + add;
if (ycopy >= && ycopy < M && field[xcopy][ycopy] == 'W') {
int idx = x * M + y;
int anotherIdx = xcopy * M + ycopy;
merge(idx, anotherIdx);
}
}
} //下
xcopy = x + ;
if (xcopy >= && x < N) {
for (int add = -; add <= ; add++) {
int ycopy = y + add;
if (ycopy >= && ycopy < M && field[xcopy][ycopy] == 'W') {
int idx = x * M + y;
int anotherIdx = xcopy * M + ycopy;
merge(idx, anotherIdx);
}
}
}
} int main()
{
cin >> N >> M;
//N = 10; M = 12; init(MAX_NUM*MAX_NUM); for (int i = ; i < N; i++) {
for (int j = ; j < M; j++) {
cin >> field[i][j];
if (field[i][j] == 'W') {
//检查上下左右八个方向是否有坑
Check(i,j);
}
}
}
set<int> s; for (int i = ; i < N; i++) {
for (int j = ; j < M; j++) {
if (field[i][j] == 'W') {
int idx = i * M + j;
//cout << "fa["<<idx << "] = "<< fa[idx] << endl;
s.insert(get(idx));
}
}
} cout << s.size() << endl; return ;
} 作者:defddr
链接:https://www.acwing.com/solution/acwing/content/3674/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
算法2
DFS 遍历 将坐标连续的坑换成. 计数+1
C++ 代码
#include <iostream> using namespace std; int N, M;
int unitCount = ; #define MAX_NUM 110 char field[MAX_NUM + ][MAX_NUM + ]; //char field[10][12] = {
// {'W','.','.','.','.','.','.','.','.','W','W','.'},
// {'.','W','W','W','.','.','.','.','.','W','W','W'},
// {'.','.','.','.','W','W','.','W','W','W','W','.'},
// {'.','.','.','.','.','.','.','.','.','W','W','.'},
// {'.','.','.','.','.','.','.','.','.','W','.','.'},
// {'.','.','W','.','.','.','.','.','.','W','.','.'},
// {'.','W','.','W','.','.','.','.','.','W','W','.'},
// {'W','.','W','.','W','.','.','.','.','.','W','.'},
// {'.','W','.','W','.','.','.','.','.','.','W','.'},
// {'.','.','W','.','.','.','.','.','.','.','W','.'}
//}; void Dfs(int x, int y)
{
//终止条件
if (x < || x >= N || y < || y >= M || field[x][y] == '.')
return; field[x][y] = '.'; Dfs(x + , y - ); Dfs(x + ,y); Dfs(x + , y + );
Dfs(x , y-); Dfs(x , y + );
Dfs(x -, y-); Dfs(x - , y); Dfs(x - , y +); } int main()
{
cin >> N >> M;
//N = 10; M = 12; for (int i = ; i < N; i++) {
for (int j = ; j < M; j++) {
cin >> field[i][j];
}
} for (int i = ; i < N; i++) {
for (int j = ; j < M; j++) {
if (field[i][j] == 'W'){
unitCount++;
Dfs(i,j);
}
}
} cout << unitCount << endl; return ;
}
POJ 2386 Lake Counting 题解《挑战程序设计竞赛》的更多相关文章
- 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 搜索题解
简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
随机推荐
- 【原创】WinForm中实现单独Time控件的方式
WinForm默认只提供了DateTimePicker,今天的项目只用时间,不能出现日期,百撕不得骑姐(^^),也没花多少时间,随便试了一下,就成功了,分享一下. 在DateTimePicker属性中 ...
- 一些实用的Django+HTML设置
一.关于引入变量 1.变量引入方法: {% block 块名称 %} <p>{{变量名}}<p> {% endblock %} 2.引入变量的值中标签是否转义: 不转义: {% ...
- dpwwn:2 Vulnhub Walkthrough
此镜像配置了静态IP地址:10.10.10.10,需要调整下网络 主机层扫描: ╰─ nmap -p1-65535 -sV -A 10.10.10.10 80/tcp open http ...
- Tornado 框架
Tronado为何物 Tornado全称Tornado Web Server,是一个用Python语言写成的Web服务器兼Web应用框架,由FriendFeed公司在自己的网站FriendFeed中使 ...
- HTML和css常见问题解答2
1.将一个块级元素水平和垂直居中有几种方法?分别是什么? 四种方式: (1).要让div等块级元素水平和垂直居中,必需知道该div等块级元素的宽度和高度,然后设置位置为绝对位置,距离页面窗口左边框和上 ...
- 2019年跨越速递Java工程师笔试题
1.下面哪个选项可以用于JSP页面之间传递对象(A C) A application B page C session D error E response 评语:这道题考察的是对JSP内置对象的了 ...
- 发布一个简单的npm包
本文简单地记录了发布一个简单npm包的过程,以便后续参考使用. 初始化npm init 通过npm init创建一个package.json文件 D:\robin\lib\weapp-utils> ...
- Git - Git简介与客户端安装
简介 Git是目前世界上最先进的分布式版本控制系统(没有之一)! 集中式版本控制系统(CVS/SVN),版本库是集中存放在中央服务器的,而一般工作的时候,用的都是自己的电脑,所以要先从中央服务器取得最 ...
- 对python中等值和大小比较
等值.大小比较 在python中,只要两个对象的类型相同,且它们是内置类型(字典除外),那么这两个对象就能进行比较.关键词:内置类型.同类型.所以,两个对象如果类型不同,就没法比较,比如数值类型的数值 ...
- Python的6种内建序列之通用操作
数据结构式通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在Python中,最基本的数据结构是序列(sequence).序列中的每 ...