POJ2653线段相交

Posted hesorchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2653线段相交相关的知识,希望对你有一定的参考价值。

题目

Pick-up sticks

二维平面给出一些棍子,问哪些棍子没被其他棍子压着。

解题思路

O ( n 2 ) O(n^2) O(n2)暴力判断即可。
因为保证了答案不超过1000,因此最坏时间复杂度是 O ( m i n ( n 2 , k n ) ) O(min(n^2,kn)) O(min(n2,kn))

代码

#include <iostream>
#include <vector>
using namespace std;

const int N = 1e5 + 5;

typedef struct Point
{
    double x, y;
    Point operator-(const Point temp)
    {
        Point T;
        T.x = x - temp.x;
        T.y = y - temp.y;
        return T;
    }
} Vector;

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

const double eps = 0;

int sign(double x)
{
    if (x < eps)
        return -1;
    else if (x > eps)
        return 1;
    return 0;
}
bool f(Point a1, Point a2, Point b1, Point b2)
{
    if (max(a1.x, a2.x) < min(b1.x, b2.x) ||
        max(a1.y, a2.y) < min(b1.y, b2.y) ||
        max(b1.x, b2.x) < min(a1.x, a2.x) ||
        max(b1.y, b2.y) < min(a1.y, a2.y))
        return 0; //先通过快速排斥实验

    double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
    double c3 = cross(b2 - b1, a2 - b1), c4 = cross(b2 - b1, a1 - b1);
    return sign(c1) * sign(c2) <= 0 && sign(c3) * sign(c4) <= 0;
}

Point Start[N], End[N];
int n;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        scanf("%lf %lf", &Start[i].x, &Start[i].y);
        scanf("%lf %lf", &End[i].x, &End[i].y);
    }
    vector<int> ans;
    for (int i = 1; i <= n; i++)
    {
        int flag = 1;
        for (int j = i + 1; j <= n; j++)
        {
            if (f(Start[i], End[i], Start[j], End[j]))
            {
                flag = 0;
                break;
            }
        }
        if (flag)
            ans.push_back(i);
    }
    int m = ans.size();
    printf("Top sticks: ");
    for (int i = 0; i < m; i++)
    {
        printf("%d", ans[i]);
        if (i == m - 1)
            printf(".");
        else
            printf(", ");
    }
    printf("\\n");
}

int main()
{
    while (scanf("%d", &n) && n)
        solve();
    return 0;
}
/*

*/

以上是关于POJ2653线段相交的主要内容,如果未能解决你的问题,请参考以下文章

POJ2653 Pick-up sticks 判断线段相交

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

POJ2653 Pick-up sticks(线段相交)

POJ_2653_Pick-up sticks_判断线段相交

POJ 2653 Pick-up sticks (线段相交)

线段判非严格相交+暴力——poj2653