地址 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 ;
}

最新文章

  1. Oracle 11g 单实例安装文档
  2. [Mongdb] 关于Replica Set复制集奇数成员限制的解释--待完善
  3. 【Selenium2+Python】定位
  4. 浅谈Oracle 性能优化
  5. ios5和ios6横竖屏支持及ipad和iphone设备的判断
  6. Qt见解:Post 与 Get 的区别(Get将参数直接与网址整合为一个整体,而Post则将其拆为两个部分)
  7. Java Web项目(Extjs)报错八
  8. Acrobat Pro DC 2019 mac中文版(pdf编辑器)
  9. flink 读取kafka 数据,partition分配
  10. python的图形模块PIL小记
  11. FWT模板(洛谷P4717 【模板】快速沃尔什变换)(FWT)
  12. jQuery应用实例4:下拉列表
  13. 每天CSS学习之transform
  14. sql表连接方式
  15. 压力测试以及编译安装httpd2.4
  16. 201709019工作日记--Java中的各种锁--未解决
  17. Day 49 CSS样式.
  18. Informatica 常用组件Expression之一 概述
  19. python基础系列教程——Python中的编码问题,中文乱码问题
  20. linux支持的内存根文件系统

热门文章

  1. JavaScript Map 和 Set
  2. Android 双屏异显的实现
  3. 收藏收藏:工作用了很久的自主开发的Sql Server代码生成器,我开源了(.NET Winform)
  4. springcloud vue.js 微服务分布式 前后分离 集成代码生成器 shiro权限 activiti工作流
  5. Git实战指南----跟着haibiscuit学Git(第六篇)
  6. js中cookie设置、获取与清除
  7. vue如何循环渲染element-ui中table内容
  8. RSA加解密&amp;RSA加验签详解
  9. MySQL解惑&mdash;&mdash;GROUP BY隐式排序
  10. python判断文件的访问权限