poj 2653 Pick-up sticks(判断线段是否相交)

//以下为原blog搬迁过来的内容

【题目大意】:给出n条木棍,然后依次摆放在桌面上,每次摆放的木棍的起始点和终止点给定,求最上面的木棍的标号

 

【解题思路】:线段判相交就是了,水题

 

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

struct Point
{
    double x, y;
    Point() {}
    Point(double a, double b)
    {
        x = a, y = b;
    }
}point[200000];

struct Line
{
    Point a, b;
    Line() {}
    Line(Point x, Point y)
    {
        a = x, b = y;
    }
}line[150000];

int judge[150000],n,m;
double p,q;

inline int sig(double k)
{
    return k < -eps ? -1 : k > eps;
}

inline double det(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}

inline double xmult(Point o, Point a, Point b)
{
    return det(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}

inline double dotdet(double x1, double y1, double x2, double y2)
{
    return x1 * x2 + y1 * y2;
}
inline double dot(Point o, Point a, Point b)
{
    return dotdet(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}

inline int between(Point o, Point a, Point b)
{
    return sig(dot(o, a, b));
}

inline int intersect1(Point a, Point b, Point c, Point d)
{
    double s1, s2, s3, s4;
    int d1 = sig(s1 = xmult(a, b, c));
    int d2 = sig(s2 = xmult(a, b, d));
    int d3 = sig(s3 = xmult(c, d, a));
    int d4 = sig(s4 = xmult(c, d, b));
    if ((d1^d2) == -2 && (d3^d4) == -2)
    {
        return 1;
    }
    if (d1 == 0 && between(c, a, b) <= 0) return 2;
    if (d2 == 0 && between(d, a, b) <= 0) return 2;
    if (d3 == 0 && between(a, c, d) <= 0) return 2;
    if (d4 == 0 && between(b, c, d) <= 0) return 2;
    return 0;
}

inline int intersect(Line u, Line v)
{
    return intersect1(u.a, u.b, v.a, v.b);
}

int main()
{
    while (~scanf("%d",&n))
    {
        if (n==0) break;
        for (int i=0; i<n; i++)
        {
            scanf("%lf%lf",&p,&q);
            point[2*i-1]=Point(p,q);
            scanf("%lf%lf",&p,&q);
            point[2*i]=Point(p,q);
            line[i]=Line(point[2*i-1],point[2*i]);
        }
        memset(judge,0,sizeof(judge));
        for (int i=0; i<n; i++)
        {
            for (int j=i+1; j<n; j++)
            {
                int k;
                k=intersect(line[i],line[j]);
                if (k!=0) {judge[i]=1; break;}
            }
        }
        printf("Top sticks:");
        for (int i=0; i<n-1; i++)
        {
            if (judge[i]==0)
            printf(" %d,",i+1);
        }
        printf(" %d.\n",n);
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值