POJ 2653 Pick-up sticks 判断两线段是否相交

http://poj.org/problem?id=2653

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.
题目大意:按照顺序在二维平面上扔木棍,最后问最上面的木棍都有哪些。(即没有木棍压着它)
思路:题目有个很关键的点就是答案不超过1000根木棍,因此我们完全可以枚举,复杂度绝对达不到 O ( n 2 ) O(n^2) O(n2)。那么问题就是怎么判断两线段相交了,其实很简单。判断线段 A B AB AB与线段 C D CD CD相交,首先判断直线 A B AB AB与线段 C D CD CD是否相交,再判断线段 A B AB AB与直线 C D CD CD是否相交,若两者同时成立则线段 A B AB AB与线段 C D CD CD相交。顺带一提,我的代码 C + + C++ C++ A C AC AC G + + G++ G++ W A WA WA,日常玄学。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;

const double pi=acos(-1.0);//弧度pi
const double eps=1e-10;//精度

struct point
{
    double x,y;
    point(double a=0,double b=0)
    {
        x=a,y=b;
    }
    friend point operator * (point a,double b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (double a,point b)
    {
        return point(b.x*a,b.y*a);
    }
    point operator - (const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    point operator + (const point &b)const
    {
        return point(x+b.x,y+b.y);
    }
    point operator / (const double b)const
    {
        return point(x/b,y/b);
    }
    bool operator < (const point &b)const//按坐标排序
    {
        if(fabs(x-b.x)<eps)
            return y<b.y-eps;
        return x<b.x-eps;
    }
    void transxy(double sinb,double cosb)//逆时针旋转b弧度
    {                                      //若顺时针 在传入的sinb前加个-即可
        double tx=x,ty=y;
        x=tx*cosb-ty*sinb;
        y=tx*sinb+ty*cosb;
    }
    void transxy(double b)//逆时针旋转b弧度
    {                     //若顺时针传入-b即可
        double tx=x,ty=y;
        x=tx*cos(b)-ty*sin(b);
        y=tx*sin(b)+ty*cos(b);
    }
    double norm()
    {
        return sqrt(x*x+y*y);
    }
};

inline double dot(point a,point b)//点积
{
    return a.x*b.x+a.y*b.y;
}
inline double cross(point a,point b)//叉积
{
    return a.x*b.y-a.y*b.x;
}

inline double dist(point a,point b)//两点间距离
{
    return (a-b).norm();
}

inline int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x>0)
        return 1;
    return -1;
}

typedef point Vector;

struct line
{
    point s,e;
    line(){}
    line(point _s,point _e)
    {
        s=_s,e=_e;
    }
    bool operator <(const line &a)const
    {
        return 1;
    }
};

double xmult(point p1,point p2,point p3)//p1p2 X p1p3
{
    return cross(p2-p1,p3-p1);
}

bool Seg_inter_line(line l1,line l2)//判断l1 与 l2 是否相交
{
    return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e))<=0;
}

int n;
line Line[100005];
int ans[1005];

int main()
{
    int len;
    double x1,y1,x2,y2;
    line tmp;
    int tot=0;
    while(~scanf("%d",&n)&&n)
    {
        tot=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            Line[i]=line(point(x1,y1),point(x2,y2));
        }
        for(int i=1;i<=n;i++)
        {
            bool flag=1;
            for(int j=i+1;j<=n;j++)
            {
                if(Seg_inter_line(Line[i],Line[j])&&Seg_inter_line(Line[j],Line[i]))
                {
                    flag=0;
                    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]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值