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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值