sicily.1004 I conduit

本文介绍了一种用于合并重叠线段的算法,该算法通过排序和遍历的方式减少绘图时所需的线段数量,适用于地图整合等场景。

1004. I Conduit!

Constraints

Time Limit: 3 secs, Memory Limit: 32 MB

Description

Irv Kenneth Diggit works for a company that excavates trenches, digs holes and generally tears up people's yards. Irv's job is to make sure that no underground pipe or cable is underneath where excavation is planned. He has several different maps, one for each utility company, showing where their conduits lie, and he needs to draw one large, consolidated map combining them all. One approach would be to simply draw each of the smaller maps one at a time onto the large map. However, this often wastes time, not to mention ink for the pen-plotter in the office, since in many cases portions of the conduits overlap with each other (albeit at different depths underground). What Irv wants is a way to determine the minimum number of line segments to draw given all the line segments from the separate maps.

Input

Input will consist of multiple input sets. Each set will start with a single line containing a positive integer n indicating the total number of line segments from all the smaller maps. Each of the next n lines will contain a description of one segment in the format x1 y1 x2 y2 where (x1,y1) are the coordinates of one endpoint and (x2,y2) are the coordinates of the other. Coordi- nate values are floating point values in the range 0... 1000 specified to at most two decimal places. The maximum number of line segments will be 10000 and all segments will have non-zero length. Following the last input set there will be a line containing a 0 indicating end of input; it should not be processed.

Output

For each input set, output on a single line the minimum number of line segments that need to be drawn on the larger, consolidated map.

Sample Input

3
1.0 10.0 3.0 14.0
0.0 0.0 20.0 20.0
10.0 28.0 2.0 12.0
2
0.0 0.0 1.0 1.0
1.0 1.0 2.15 2.15
2
0.0 0.0 1.0 1.0
1.0 1.0 2.15 2.16
0

Sample Output

2
1


/*
 * 主要对所有的线段进行排序,先按k排,在按b排,最后按线段的最小一个坐标x排(从小到大排)
 * 如果线段没有斜率,将k置为最大INF
 *
 */

#include <iostream>
#include <algorithm>
using namespace std;
const double INF = 1e+15;
const double EPS = 1e-7;

// 比较 两个数的大小
int cmp(double a, double b)
{
	if (a - b < -EPS)
		return -1;
	if (a - b > EPS)
		return 1;
	return 0;
}

// 线段结构体
struct Line
{
	double begin, end, k, b;
	Line() { }
	Line(double x1, double y1, double x2, double y2)
	{
		if (cmp(x1, x2) == 0) { // 无斜率情况
			k = INF;
			b =	x1;
			begin = min(y1, y2);
			end = max(y1, y2);
		} else { // 有斜率情况
			k = (y2 - y1)/(x2 - x1);
			b = y1 - k * x1;
			begin = min(x1, x2);
			end = max(x1, x2);
		}
	}
} line[10000];

// 比较两条线段,从k、b、begin的顺序比下去(降序)
bool cmpline(Line a, Line b)
{
	if (cmp(a.k, b.k) != 0)
		return cmp(a.k, b.k) < 0;
	if (cmp(a.b, b.b) != 0)
		return cmp(a.b, b.b) < 0;
	return cmp(a.begin, b.begin) < 0;
}

int main()
{
	int n;
	double x1, y1, x2, y2;
	while (cin >> n && n != 0)
	{
		for (int i = 0; i < n; i++) {
			cin >> x1 >> y1 >> x2 >> y2;
			line[i] = Line(x1, y1, x2, y2);
		}

		// 对所有线段按k、b、begin的顺序排序下去(降序)
		sort(line, line + n, cmpline);

		//总线段
		int amount_of_line = n;
		// 第一条线段的end值
		double max_end = line[0].end;

		// 从第二条线段开始遍历
		for (int i = 1; i < n; i++)
		{
			if (cmp(line[i].k, line[i - 1].k) == 0 && cmp(line[i].b, line[i - 1].b) == 0) {
				if (cmp(line[i].begin, max_end) <= 0)
					amount_of_line--;  // 可以用一条线段表示则将amount_of_line减1
				max_end = max(max_end, line[i].end); // 刷新max_end值
			} else {
				max_end = line[i].end; // 刷新max_end值
			}
		}

		cout << amount_of_line << endl;
	}

	return 0;
}


先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值