# i conduct

268人阅读 评论(0)

1. 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
2

CPP

#include <iostream>
#include <cmath>
#include <algorithm>

double const MAX = 99999;

using namespace std;

struct seg{//结构体的标签
double k;
double b;
double leftx;
double downy;
double rightx;
double upy;
}l[10001];//一个数组；记住如何定义结构体

void swap(double& a, double& b) {
double tmp = a;
a = b;
b = tmp;
}
／＊void swap(double *a, double *b) {
double tmp = *a;
a = b;
b = &tmp;
用c来表示swap函数
}＊／

bool equal(double a, double b) {
return fabs(a-b) < 1e-6;
}//浮点数有误差，当误差小于某个数时，认为这两个数是相等的。

bool comseg(const seg& a, const seg& b) {
if(equal(a.k, b.k)) {
if(a.k == MAX) {
if(equal(a.b, b.b)) {
if(equal(a.downy, b.upy)) {
return a.upy < b.upy;
}
return a.downy < b.downy;
}
return a.b < b.b;
}
else if(equal(a.b, b.b)) {
if(equal(a.leftx, b.leftx)) {
return a.rightx < b.rightx;
}
return a.leftx < b.leftx;
}
return a.b < b.b;
}
return a.k < b.k;
}//用于排序！

int main() {
int n;
while(cin >> n && n != 0) {
for(int i = 0; i < n; i++) {
cin >> l[i].leftx >> l[i].downy >> l[i].rightx >> l[i].upy;
if(l[i].leftx == l[i].rightx) {
l[i].k = MAX;
l[i].b = l[i].leftx;
if(l[i].downy > l[i].upy) {
swap(l[i].downy, l[i].upy);//让up和right为大的数。
}
}
else if(l[i].leftx > l[i].rightx) {
l[i].k = (l[i].upy - l[i].downy) / (l[i].rightx - l[i].leftx);
l[i].b = l[i].downy - l[i].leftx  * l[i].k;
swap(l[i].leftx, l[i].rightx);
swap(l[i].downy, l[i].upy);
}
else {
l[i].k = (l[i].upy - l[i].downy) / (l[i].rightx - l[i].leftx);
l[i].b = l[i].downy - l[i].leftx  * l[i].k;
}
}
sort(l, l+n, comseg);
int count = 1;
double x = l[0].rightx;
double y = l[0].upy;
for(int i = 1; i < n; i++) {
if(!(equal(l[i].k, l[i-1].k) && equal(l[i].b, l[i-1].b))){//注意!的作用域！
count++;
}
else if((l[i].k == MAX && l[i].downy > y) || (l[i].k != MAX && l[i].leftx > x)) {
count++;
}
else if((l[i].k == MAX && y > l[i].upy) || (l[i].k != MAX && x >= l[i].rightx)) {
continue;
}
x = l[i].rightx;
y = l[i].upy;
}
cout << count << endl;
}
return 0;
}


1一定要用排序！可以减少很多的问题，只要比较相邻两条线就可以了。
2就算k和d相等，也有需要比较的部分。
3注意浮点数的误差问题

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：131655次
• 积分：3158
• 等级：
• 排名：第10912名
• 原创：187篇
• 转载：16篇
• 译文：2篇
• 评论：18条
文章分类
评论排行
最新评论