地址 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 题解《挑战程序设计竞赛》的更多相关文章

  1. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  2. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  3. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  4. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  5. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  6. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  7. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

  8. POJ 2386 Lake Counting 搜索题解

    简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...

  9. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

随机推荐

  1. centos开启ftp服务

    新安装的要先配置网络 $_> vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 最后一行 onboot = yes $_> yum i ...

  2. vue - Error: Can't resolve '@/assets/img/github.svg (vue-cli3.0,无法解析.svg图片,已解决)

    用vue脚手架(vue-cli3.0)生成的目录,无法解析.svg图片的问题 <img src="@/assets/img/github.svg" alt="git ...

  3. std::map自定义类型key

    故事背景:最近的需求需要把一个结构体struct作为map的key,时间time作为value,定义:std::map<struct, time> _mapTest; 技术调研:众所周知, ...

  4. maven与gradle的阿里云仓库配置

    直接参考 https://help.aliyun.com/document_detail/102512.html 就好. 阿里云maven仓库官网 https://maven.aliyun.com/m ...

  5. 10-Node.js学习笔记-异步函数

    异步函数 异步函数是异步编程语法的终极解决方案,它可以让我们将异步代码写成同步的形式,让代码不再有回调函数嵌套,是代码变得清晰明了 const fn = async()=>{} async fu ...

  6. Java面试准备基础篇_11.24

    Java类加载机制 Java内存模型JMM 为什么 Redis 单线程能支撑高并发? 高并发下的接口幂等性解决方案! 面试官问:平常你是怎么对 Java 服务进行调优的? JAVA虚拟机(JVM)六: ...

  7. 【bzoj4555】[Tjoi2016&Heoi2016]求和(NTT+第二类斯特林数)

    传送门 题意: 求 \[ f(n)=\sum_{i=0}^n\sum_{j=0}^i\begin{Bmatrix} i \\ j \end{Bmatrix}2^jj! \] 思路: 直接将第二类斯特林 ...

  8. CALL和RET指令实验

    实验10 1.在屏幕8行3列,用绿色显示data段中的字符串 assume cs:code data segment db data ends code segment start: ;行 ;列 ;颜 ...

  9. 关于AttributeError: 'NoneType' object has no attribute 'send_keys'

    在学web自动化测试时,通过PO模型将特定页面的一些元素及元素操作放在特定页面模块中, 然后提取公共的部分, 如元素等待WebDriverWait, 元素操作send_keys, click, 获取元 ...

  10. 【python之路.一】基础

    数学操作符 数据类型 字符串复制(*复制次数int).连接(+) 该类操作只能同为字符串类型,否则需要强制转换类型 变量名规则 (驼峰式变量名&下划线式均可) # 注释 BIF(built-i ...