地址 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. 一起学SpringMVC之国际化

    随着网络的发展,在Web开发中,系统的国际化需求已经变得非常的普遍.本文主要讲解SpringMVC框架对多语言的支持,仅供学习分享使用,如有不足之处,还请指正. 什么是国际化? 国际化(interna ...

  2. sklearn集成支持向量机svm.SVC参数说明

    经常用到sklearn中的SVC函数,这里把文档中的参数翻译了一些,以备不时之需. 本身这个函数也是基于libsvm实现的,所以在参数设置上有很多相似的地方.(PS: libsvm中的二次规划问题的解 ...

  3. Android WebView与H5联调技巧

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/78 背景: 突然想写一篇关于Android WebView ...

  4. Spring Boot修改JSP不用重启的办法

    在application.properties文件中添加一行代码解决. Spring Boot 2.0以上添加如下一行: server.servlet.jsp.init-parameters.deve ...

  5. 连接 sql

    java连接sqlserver 1 创建 Dynamic Web Project项目 在WebContent/WEB-INF/lib中添加sqljdbc42.jar 2 在class文件里连接数据库 ...

  6. 如何写一个Python万能装饰器,既可以装饰有参数的方法,也可以装饰无参数方法,或者有无返回值都可以装饰

    Python中的装饰器,可以有参数,可以有返回值,那么如何能让这个装饰器既可以装饰没有参数没有返回值的方法,又可以装饰有返回值或者有参数的方法呢?有一种万能装饰器,代码如下: def decorate ...

  7. JavaScript-----13.内置对象 Math()和Date()

    1. 内置对象 js对象分为3种:自定义对象(var obj={}).内置对象.浏览器对象. 前两种对象是js基础内容,属于ECMAScript,第三个浏览器对象是js独有的.讲js API的时候会讲 ...

  8. java之等待唤醒机制(线程之间的通信)

    线程间通信 概念:多个线程在处理同一个资源,但是处理的动作(线程的任务)却不相同.比如:线程A用来生成包子的,线程B用来吃包子的,包子可以理解为同一资源,线程A与线程B处理的动作,一个是生产,一个是消 ...

  9. 如何获得大学教材的PDF版本?

    最近急需一本算法书的配套答案,这本配套单独出售,好像在市面上还买不到,在淘宝上搜索也只是上一个版本,并没有最新版本,让我很无奈.加上平时肯定会有这么一种情况,想看一些书,但买回来也看不了几次,加上计算 ...

  10. Java每日一面(Part1:计算机网络)[19/10/21]

    作者:故事我忘了¢个人微信公众号:程序猿的月光宝盒 1.UDP简介 1.1UDP报文结构: ​ Source Port:源端口 Destination Port:目标端口 Length:数据包长度 C ...