POJ 2653 (判断线段相交)

来源:互联网 发布:js 全局变量 丢失属性 编辑:程序博客网 时间:2024/04/29 16:02

Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14044 Accepted: 5328
Description

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.
Source

题意:给N个棍子,和它们的起始点坐标,后输入的棍子在先输入的上面,输出在top的所有棍子编号

做法:线段相交,就是把两条线段分别看成直线和线段来判断。
和判断直线和线段相交差不多

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <map>#include <stack>#include <vector>#define maxn 100010#define maxe 100010typedef long long ll;using namespace std;const double eps=1e-5;const int inf=0x3f3f3f3f3f;typedef double T1;vector<int > ans;struct Point{    T1 x,y;    Point(){};    Point(T1 a,T1 b)    {        x=a,y=b;    }    void input()    {        scanf("%lf%lf",&x,&y);    }    Point operator +(Point a)    {        Point b(x+a.x,y+a.y);        return b;    }    Point operator -(Point a)    {        Point b(x-a.x,y-a.y);        return b;    }    T1 operator *(Point a)    {        return x*a.x+y*a.y;    }    T1 operator ^(Point a)    {        return x*a.y-y*a.x;    }    bool operator <(Point a)    {        return x<a.x;    }};double mul(Point p1,Point p2,Point p3){    return (p2-p1)^(p3-p1);}struct Line{    Point a,b;    double k,d;    bool flag;    bool top;    void prepare()    {        if(a.x-b.x!=0)        k=(a.y-b.y)*1.0/(a.x-b.x),flag=true,d=a.y-k*a.x;        else flag=false,d=a.x;        top=true;    }    Line(Point st,Point ed)    {        a=st;        b=ed;        prepare();    }    Line(){};    void input()    {        a.input();        b.input();        prepare();    }    int operator *(Line l)    {        return (a-b)*(l.a-l.b);    }    int operator ^( Line l)    {        return (a-b)^(l.a-l.b);    }    Point intersect(Line l)    {        Point r;double x,y;        if(!flag&&l.flag)        {            x=d;            y=l.k*x+l.d;        }        else if(flag&&!l.flag)        {            x=l.d;            y=k*x+d;        }        else        {            x=(l.d-d)/(k-l.k);            y=k*x+d;        }        printf("POINT %.2lf %.2lf\n",x,y);        r.x=x;        r.y=y;        return r;    }    bool inter(Line l)    {        return mul(a,l.a,l.b)*mul(b,l.a,l.b)<=0&&        mul(l.a,a,b)*mul(l.b,a,b)<=0;    }}line[maxn];int main(){    int n;    while(scanf("%d",&n)==1&&n)    {        ans.clear();        for(int i=1;i<=n;i++)        {            line[i].input();        }        for(int i=1;i<=n;i++)        {            for(int j=i+1;j<=n;j++)            {                if(line[i].inter(line[j]))                {                    line[i].top=false;                    break;                }            }            if(line[i].top)ans.push_back(i);        }        printf("Top sticks: ");        int siz=ans.size();        for(int i=0;i<siz-1;i++)        {            printf("%d, ",ans[i]);        }        printf("%d.\n",ans[siz-1]);    }    return 0;}
原创粉丝点击