hdu 1147 Pick-up sticks(求是否有交点)

题目地址

题目大意:按顺序给出n条线段的端点坐标,判断每条坐标是否在线段堆的最上面,按输入顺序输出在最上端的线段的编号

解题思路:顺序判断该条线段与之后输入的线段是否有交点

#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <algorithm>  
#include <cstring>  
#include <queue>  
#include <string>  
#include <map>  
#include <stack>  
#include <list>  
#include <set>

using namespace std;

const int maxn = 1e5+10;
const int eps = 1e-9;

int ans[maxn];

struct Point
{
    double x,y;
    Point(double x = 0, double y = 0) : x(x),y(y){}
}p1[maxn],p2[maxn];

typedef Point Vector;

Vector operator - (Point A, Point B)
{
    return Vector(A.x-B.x, A.y-B.y);
}

double Cross(Vector A,Vector B)
{
    return A.x * B.y - A.y * B.x;
}

int dcmp(double x)
{
    if(fabs(x) < eps)
        return 0;
    else
        return x > 0 ? 1 : -1;
}

bool SegmentProperIntersection(Point A,Point B,Point C,Point D)
{
    double c1 = Cross(B-A,C-A);
    double c2 = Cross(B-A,D-A);
    double c3 = Cross(D-C,A-C);
    double c4 = Cross(D-C,B-C);
    return dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 0;
}

int main()
{
    int n;
    while(scanf("%d",&n))
    {
        if(!n)  break;
        for(int i = 1; i <= n; i++)
            scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
        int tag;
        int cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            tag = -1;
            for(int j = i+1; j <= n; j++)
            {
                if(SegmentProperIntersection(p1[i],p2[i],p1[j],p2[j]))
                {
                    tag = j;
                    break;
                }
            }
            if(tag == -1)
                ans[cnt++] = i;
        }
        printf("Top sticks: ");
        for(int i = 0; i < cnt-1; i++)
            printf("%d, ",ans[i]);
        printf("%d.\n",ans[cnt-1]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值