poj 1127 线段相交的判定

来源:互联网 发布:淘宝可可里小姐扒皮 编辑:程序博客网 时间:2024/05/09 11:08

题意:

有n根木棍,每根的端点坐标分别是 px, py, qx, qy。

判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。

并且通过相连的木棍相连的木棍也是相连的。


解析:

线段相交的判定。

首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定;

然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。

最后,本身与本身是相连的,否则wa。


代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = acos(-1.0);const double ee = exp(1.0);/////////////////////////////////////////////////////struct Point{    double x, y;    Point(double x = 0, double y = 0) : x(x), y(y) {}};bool cmp(Point A, Point B){    if (A.x == B.x)        return A.y < B.y;    return A.x < B.x;}typedef Point Vector;Vector operator + (Vector A, Vector B){    return Vector(A.x + B.x, A.y + B.y);}Vector operator - (Point A, Point B){    return Vector(A.x - B.x, A.y - B.y);}Vector operator * (Vector A, double p){    return Vector(A.x * p, A.y * p);}Vector operator / (Vector A, double p){    return Vector(A.x / p, A.y / p);}//便于点排序bool operator < (const Point& a, const Point& b){    return a.x < b.x || (a.x == b.x && a.y < b.y);}//用于判断相等int dcmp(double x){    if (fabs(x) < eps)    {        return 0;    }    else    {        return x < 0 ? -1 : 1;    }}bool operator == (const Point& a, const Point& b){    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}//点积double Dot(Vector A, Vector B){    return A.x * B.x + A.y * B.y;}//向量长度double Length(Vector A){    return sqrt(Dot(A, A));}//向量夹角double Angle(Vector A, Vector B){    return acos(Dot(A, B) / Length(A) / Length(B));}//叉集double Cross(Vector A, Vector B){    return A.x * B.y - A.y * B.x;}//求两个向量相夹的面积double Area2(Point A, Point B, Point C){    return Cross(B - A, C - A);}//旋转rad弧度Vector Rotate(Vector A, double rad){    return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));}//向量的单位法线即向量左转90°Vector Normal(Vector A){    double L = Length(A);    return Vector(-A.y / L, A.x / L);}//求交点坐标Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){    Vector u = P - Q;    double t = Cross(w, u) / Cross(v, w);    return P + v * t;}//点P到直线AB的距离double DistanceToLine(Point P, Point A, Point B){    Vector v1 = B - A;    Vector v2 = P - A;    return fabs(Cross(v1, v2)) / Length(v1);}//点到直线的距离double DistanceToSegment(Point P, Point A, Point B){    if (A == B)    {        return Length(P - A);    }    Vector v1 = B - A;    Vector v2 = P - A;    Vector v3 = P - B;    if (dcmp(Dot(v1, v2)) < 0)        return Length(v2);    else if (dcmp(Dot(v1, v3)) > 0)        return Length(v3);    else        return fabs(Cross(v1, v2)) / Length(v1);}//判断线段是否相交bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){    double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);    double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;}//线段是否在端点相交 + 线段相交的判定bool OnSegment(Point p, Point a1, Point a2){    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;}//传入顶点集 计算多边形的面积double ConvexPolygonArea(Point* p, int n){    double area = 0;    for (int i = 1; i < n - 1; i++)    {        area += Cross(p[i] - p[0], p[i + 1] - p[0]);    }    return area / 2.0;}//° -> 弧度double torad(double deg){    return deg / 180 * pi;}//求凸包 返回点个数 ch为凸包的点int ConvexHull(Point* p, int n, Point* ch){    sort(p, p + n, cmp);    int m = 0;    for (int i = 0; i < n; i++)    {        while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)            m--;        ch[m++] = p[i];    }    int k = m;    for (int i = n - 2; i >= 0; i--)    {        while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)            m--;        ch[m++] = p[i];    }    if (n > 1)        m--;    return m;}Point readPoint(){    double x, y;    scanf("%lf %lf", &x, &y);    return Point(x, y);}//////////////////////////////////////////////////////Point p1[20];Point p2[20];int n;bool ok(int i, int j){    return (SegmentProperIntersection(p1[i], p2[i], p1[j], p2[j])    || OnSegment(p1[i], p1[j], p2[j]) ||    OnSegment(p2[i], p1[j], p2[j]) ||    OnSegment(p1[j], p1[i], p2[i]) ||    OnSegment(p2[j], p1[i], p2[i])            || p1[i] == p1[j] || p1[i] == p2[j] || p2[i] == p1[j] || p2[i] == p2[j]);}int g[20][20];void floyd(){    for (int k = 0; k < n; k++)    {        for (int i = 0; i < n; i++)        {            for (int j = 0; j < n; j++)            {                if (g[i][k] && g[k][j])                    g[i][j] = 1;            }        }    }}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);#endif // LOCAL    while (~scanf("%d", &n) && n)    {        memset(g, 0, sizeof(g));        for (int i = 0; i < n; i++)        {            p1[i] = readPoint();            p2[i] = readPoint();            g[i][i] = 1;        }        for (int i = 0; i < n; i++)        {            for (int j = i + 1; j < n; j++)            {                if (ok(i, j))                {                    g[i][j] = 1;                    g[j][i] = 1;                }            }        }        floyd();        int a, b;        while (scanf("%d%d", &a, &b))        {            if (!a && !b)                break;            puts(g[a - 1][b - 1] ? "CONNECTED" : "NOT CONNECTED");        }    }    return 0;}


0 0
原创粉丝点击