• POJ 2074 Line of Sight


     POJ_2074

        首先来讲,我们能够看全房子的视野构成的平面一定是被某两个障碍物的端点卡住的(将Property Line的两个端点也视作障碍物的端点),否则我们的视野一定可以继续向两侧延伸。

        于是,我们就可以枚举任意两个端点,然后计算出视野与Property Line相交的区间,然后再枚举所有障碍物,如果没有障碍物在这个视野之内,那么这个视野就是可行的,然后更新区间的范围即可。

        此外,对于Property Line上最长区间长度为0的情况不必纠结,怎么处理都可以,经测试后认为题目应该没有这样的数据。

        最后说明一下,这个题没有给出数据范围,但依网上的解题报告来看,障碍物的数量应该是不超过100个的。

    #include<stdio.h>
    #include<string.h>
    #define MAXD 220
    #define zero 1e-8
    int N, P;
    double hx1, hx2, hy, px1, px2, py, x1[MAXD], x2[MAXD], h[MAXD], y[MAXD], x[MAXD], ans;
    double fabs(double x)
    {
    return x < 0 ? -x : x;
    }
    int dcmp(double x)
    {
    if(fabs(x) < zero)
    return 0;
    if(x < 0)
    return -1;
    return 1;
    }
    double det(double x1, double y1, double x2, double y2)
    {
    return x1 * y2 - x2 * y1;
    }
    void init()
    {
    int i, j, k;
    scanf("%lf%lf%lf", &px1, &px2, &py);
    scanf("%d", &N);
    for(i = 0; i < N; i ++)
    scanf("%lf%lf%lf", &x1[i], &x2[i], &h[i]);
    P = 0;
    x[P] = px1, y[P] = py, ++ P;
    x[P] = px2, y[P] = py, ++ P;
    for(i = 0; i < N; i ++)
    if(dcmp(h[i] - hy) < 0 && dcmp(h[i] - py) > 0)
    {
    x[P] = x1[i], y[P] = h[i], ++ P;
    x[P] = x2[i], y[P] = h[i], ++ P;
    }
    }
    void calculate(int k1, int k2)
    {
    int i, j, k;
    double t, t1, t2, left1, left2, right1, right2, left, right;
    t = det(x[k1] - hx1, y[k1] - hy, x[k2] - hx1, y[k2] - hy);
    if(dcmp(t) < 0)
    k = k1, k1 = k2, k2 = k;
    left1 = (py - hy) * (x[k1] - hx1) / (y[k1] - hy) + hx1;
    right1 = (py - hy) * (x[k2] - hx1) / (y[k2] - hy) + hx1;
    t = det(x[k1] - hx2, y[k1] - hy, x[k2] - hx2, y[k2] - hy);
    if(dcmp(t) < 0)
    k = k1, k1 = k2, k2 = k;
    left2 = (py - hy) * (x[k1] - hx2) / (y[k1] - hy) + hx2;
    right2 = (py - hy) * (x[k2] - hx2) / (y[k2] - hy) + hx2;
    right = right1 < right2 ? right1 : right2;
    right = right > px2 ? px2 : right;
    left = left1 > left2 ? left1 : left2;
    left = left < px1 ? px1 : left;
    t = right - left;
    if(dcmp(t) < 0)
    return ;
    for(i = 0; i < N; i ++)
    if(dcmp(h[i] - hy) < 0 && dcmp(h[i] - py) > 0)
    {
    t1 = det(left - hx1, py - hy, x2[i] - hx1, h[i] - hy);
    t2 = det(right - hx2, py - hy, x1[i] - hx2, h[i] - hy);
    if(dcmp(t1) > 0 && dcmp(t2) < 0)
    return ;
    }
    if(t > ans)
    ans = t;
    }
    void solve()
    {
    int i, j, k;
    ans = -1;
    for(i = 0; i < P; i ++)
    for(j = i + 1; j < P; j ++)
    calculate(i, j);
    if(dcmp(ans) < 0)
    printf("No View\n");
    else
    printf("%.2lf\n", ans);
    }
    int main()
    {
    for(;;)
    {
    scanf("%lf%lf%lf", &hx1, &hx2, &hy);
    if(dcmp(hx1) == 0 && dcmp(hx2) == 0 && dcmp(hy) == 0)
    break;
    init();
    solve();
    }
    return 0;
    }


  • 相关阅读:
    类似最长递增子序,记忆化DP—— Codeforces Beta Round #4 (Div. 2 Only)D Mysterious Present
    最小逆序数对——hdu1394
    区间更新 求总区间——hdu1754
    抽象类 虚函数实现
    poj2271
    poj2246
    poj2410
    poj2567
    poj2247
    Integration Services 学习(7):包部署 (转自游子吟)
  • 原文地址:https://www.cnblogs.com/staginner/p/2350205.html
Copyright © 2020-2023  润新知