【跨立实验】poj2653

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.

Input

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

Output

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown. 

The picture to the right below illustrates the first case from input.

Sample Input

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0

Sample Output

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.

题目大意:有n根棍子丢在地上,求最上面的木棍的编号

由于棍子是丢在地上的,所有需要判断非严格线段相交

就是如同T字型的话也认为是相较

判断线段相交主要是跨立实验,当符合跨立实验的时候,无需再进行快速排斥实验

其中非严格相交(可以T字形)

//非严格相交,可以T字形,如果相交则返回true,非严格相交与严格相交互不包含
bool judge2(Line a,Line b){
    double ans1=cross_product(Point(b.a.x-a.a.x,b.a.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))*
            cross_product(Point(b.b.x-a.a.x,b.b.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y));
    double ans2=cross_product(Point(a.a.x-b.a.x,a.a.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))*
            cross_product(Point(a.b.x-b.a.x,a.b.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y));
    if(fabs(ans1)<=eps&&ans2<-eps||fabs(ans2)<=eps&&ans1<-eps){
        return true;
    }
    return false;
}

严格相交的如下

//严格相交,如果相交则返回true
bool judge(Line a,Line b) {
    if(cross_product(Point(b.a.x-a.a.x,b.a.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))*
            cross_product(Point(b.b.x-a.a.x,b.b.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))<=-eps
            &&cross_product(Point(a.a.x-b.a.x,a.a.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))*
            cross_product(Point(a.b.x-b.a.x,a.b.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))<=-eps)
        return true;
    else
        return false;
}

因为T字形的时候,会出现一个叉积为异号,另外一个为0的情况

该题最后代码如下

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
#define eps 1e-8
struct Point {
    double x,y;
    Point() {}
    Point(double _x,double _y) {
        x=_x;
        y=_y;
    }
};
double cross_product(Point a,Point b) { //求点a和点b的叉积
    return a.x*b.y-a.y*b.x;
}
struct Line {
    Point a,b;
    Line() {}
    Line(Point _a,Point _b) {
        a=_a;
        b=_b;
    }
}p[100005];
bool judge(Line a,Line b) {//严格相交,如果相交则返回true
    if(cross_product(Point(b.a.x-a.a.x,b.a.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))*
            cross_product(Point(b.b.x-a.a.x,b.b.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))<=-eps
            &&cross_product(Point(a.a.x-b.a.x,a.a.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))*
            cross_product(Point(a.b.x-b.a.x,a.b.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))<=-eps)
        return true;
    else
        return false;
}
bool judge2(Line a,Line b){//非严格相交,可以T字形,如果相交则返回true,非严格相交与严格相交互不包含
    double ans1=cross_product(Point(b.a.x-a.a.x,b.a.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))*
            cross_product(Point(b.b.x-a.a.x,b.b.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y));
    double ans2=cross_product(Point(a.a.x-b.a.x,a.a.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))*
            cross_product(Point(a.b.x-b.a.x,a.b.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y));
    if(fabs(ans1)<=eps&&ans2<-eps||fabs(ans2)<=eps&&ans1<-eps){
        return true;
    }
    return false;
}
int ans[100005],tot;
int main() {
    int n;
    while(~scanf("%d",&n)) {
        tot=0;
        if(!n)break;
        for(int i=1; i<=n; i++){
            scanf("%lf%lf%lf%lf",&p[i].a.x,&p[i].a.y,&p[i].b.x,&p[i].b.y);
        }
        for(int i=1; i<=n; i++) {
            bool flag=0;
            for(int j=i+1; j<=n; j++)
                if(judge(p[i],p[j])||judge2(p[i],p[j])) {
                    flag=1;
                    break;
                }
            if(!flag)ans[++tot]=i;
        }
        printf("Top sticks: ");
        for(int i=1; i<tot; i++)printf("%d, ",ans[i]);
        printf("%d.\n",ans[tot]);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值