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
1


这道题一开始没想到要排列,排列是关键。按照k,b和start端点的顺序的排。这里要分情况,如果有斜率的话,端点就是x1和x2,因为当斜率为0的时候,y=0。如果没有斜率k=无穷大,b为x值,端点为y1和y2。还有就是注意精度的问题,因为不同的点对虽然k和b相同,但计算出来的k和b总是有误差,当两个k误差很小的时候就可以认为两个k相同。


#include <bits/stdc++.h>
#define eps 1e-7
using namespace std;
struct line{
    double start,end;
    double k,b;
    line(){}
    line(double start,double end,double k,double b):start(start),end(end),k(k),b(b){}
}Line[10002];
bool cmp1(line t1,line t2){
    //k不相同就按从小到大的顺序排列 
    if(fabs(t1.k-t2.k)>eps){
        return t1.k<t2.k;
    }
    //k相同就按b的大小顺序排序
    if(fabs(t1.b-t2.b)>eps) {
        return t1.b<t2.b;
    }
    //按照start的大小顺序排列 
    return t1.start<t2.start;
} 
int main(){
    int n;
    while(scanf("%d",&n)&&n){
        int ans=n;
        int countt=0;
        double x1,y1,x2,y2,k,b,start,end;
        for(int i=0;i<n;i++){
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            if(x1!=x2){
                k=(y1-y2)/(x1-x2);
                b=y1-k*x1;
                start=min(x1,x2);
                end=max(x1,x2);
            }
            else{
                k=1E9;
                b=x1;
                start=min(y1,y2);
                end=max(y1,y2);
            }
            Line[countt++]=line(start,end,k,b);
        }
        sort(Line,Line+countt,cmp1);
        for(int i=0;i<n-1;i++){
            //说明两条线段是同一条直线生成的 
            if(fabs(Line[i].k-Line[i+1].k)<=eps&&fabs(Line[i].b-Line[i+1].b)<=eps&&Line[i+1].start<=Line[i].end){
                ans--;
                Line[i+1].end=max(Line[i].end,Line[i+1].end);
            }
            
        }
        printf("%d\n",ans);
        
    }
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值