欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj2653 Pick-up sticks(判断线段相交)

程序员文章站 2022-06-03 19:29:09
...

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.
poj2653 Pick-up sticks(判断线段相交)
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根木棍编号1到n,如果一根棍子上面没有被其它棍子压着,则称为Top sticks,让你从小到大输出Top sticks的编号。

思路:暴力枚举下编号为i的木棍与后面i+1到n号木棍,判断下它们是否相交

代码如下

#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const double eps=1e-8;
const double INF=0x3f3f3f3f;

int dcmp(double x) 
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
struct Point 
{
    double x,y;
    Point() {}
    Point(double  _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator-(const Point &b) const {
        return Point(x-b.x,y-b.y);
    }
    double operator *(const Point &b)const {
        return x*b.x + y*b.y;
    }
    double operator ^(const Point &b)const {
        return x*b.y - y*b.x;
    }
};
struct Line 
{
    Point a,b;
    Line() {}
    Line(Point _a,Point _b) 
    {
        a=_a;
        b=_b;
    }
};
const int N=1e5+5;

Line line[N];
int vis[N];

double xmult(Point p0,Point p1,Point p2) 
{
    return (p1-p0)^(p2-p0);
}
bool inter(Line L1,Line L2)//判断两条线段是否相交 
{
    return
        max(L1.a.x,L1.b.x) >= min(L2.a.x,L2.b.x) &&
        max(L2.a.x,L2.b.x) >= min(L1.a.x,L1.b.x) &&
        max(L1.a.y,L1.b.y) >= min(L2.a.y,L2.b.y) &&
        max(L2.a.y,L2.b.y) >= min(L1.a.y,L1.b.y) &&
        dcmp((L2.a-L1.a)^(L1.b-L1.a))*dcmp((L2.b-L1.a)^(L1.b-L1.a)) <= 0 &&
        dcmp((L1.a-L2.a)^(L2.b-L2.a))*dcmp((L1.b-L2.a)^(L2.b-L2.a)) <= 0; 
}

int main() 
{
    int n;
    double x1,y1,x2,y2;
    while(scanf("%d",&n)!=EOF&&n)
    {
        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));
            vis[i]=1;
        }
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        if(inter(line[i],line[j]))
        {
            vis[i]=0;
            break;
        }
        printf("Top sticks: ");
        int  flag = 1;
        for(int i=1;i<=n;i++)
        if(vis[i])
        {
            if(flag)
                flag=0;
            else 
                printf(", ");
            printf("%d",i);
        }
        printf(".\n");
    }
    return 0;
}