zoukankan      html  css  js  c++  java
  • poj1127

    floyd+几何计算

    线段相交

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    using namespace std;

    #define maxn 20
    #define zero(x) (((x)>0?(x):-(x))<eps)
    #define eps 1.0E-8

    struct Point
    {
    double x, y;
    };

    struct Line
    {
    Point a, b;
    } straw[maxn];

    bool g[maxn][maxn];
    int n;

    void input()
    {
    for (int i = 0; i < n; i++)
    scanf(
    "%lf%lf%lf%lf", &straw[i].a.x, &straw[i].a.y, &straw[i].b.x, &straw[i].b.y);
    }

    double xmult(Point p1, Point p2, Point p0)
    {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
    }

    int dots_inline(Point p1, Point p2, Point p3)
    {
    return zero(xmult(p1, p2, p3));
    }

    int same_side(Point p1, Point p2, Line l)
    {
    return xmult(l.a, p1, l.b) * xmult(l.a, p2, l.b) > eps;
    }

    int dot_online_in(Point p, Line l)
    {
    return zero(xmult(p, l.a, l.b)) && (l.a.x - p.x) * (l.b.x - p.x) < eps && (l.a.y - p.y) * (l.b.y - p.y) < eps;
    }

    int intersect_in(Line u, Line v)
    {
    if (!dots_inline(u.a, u.b, v.a) || !dots_inline(u.a, u.b, v.b))
    return !same_side(u.a, u.b, v) && !same_side(v.a, v.b, u);
    return dot_online_in(u.a, v) || dot_online_in(u.b, v) || dot_online_in(v.a, u) || dot_online_in(v.b, u);
    }

    void make()
    {
    for (int i = 0; i < n; i++)
    g[i][i]
    = true;
    for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
    if (intersect_in(straw[i], straw[j]))
    g[i][j]
    = g[j][i] = true;
    }

    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]
    = true;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    while (scanf("%d", &n), n)
    {
    memset(g,
    0, sizeof(g));
    input();
    make();
    floyd();
    int a, b;
    while (scanf("%d%d", &a, &b), a | b)
    {
    a
    --;
    b
    --;
    if (g[a][b])
    printf(
    "CONNECTED\n");
    else
    printf(
    "NOT CONNECTED\n");
    }
    }
    return 0;
    }

  • 相关阅读:
    Java面试题
    删除两个相同的数据
    Oracle中的rowid rownum
    SQL的七种连接
    Oracle的分页和MySQL的分页
    Script to Collect Log File Sync Diagnostic Information (lfsdiag.sql) (文档 ID 1064487.1)
    Analytic Functions in Oracle
    Oracle X$Tables
    Microsoft SQL Server Trace Flags
    Oracle Log Block Size
  • 原文地址:https://www.cnblogs.com/rainydays/p/2181770.html
Copyright © 2011-2022 走看看