zoukankan      html  css  js  c++  java
  • hoj1392

    凸包问题,求出凸包后,计算距离,不知道为什么n==2的情况居然不是两点距离的2倍,而是正好是两点的距离。

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

    #define maxn 505

    struct point
    {
    double x, y;
    }pnt[maxn], res[maxn];

    int n, m;

    bool mult(point sp, point ep, point op)
    {
    return (sp.x - op.x) * (ep.y - op.y)>= (ep.x - op.x) * (sp.y - op.y);
    }

    bool operator < (const point &l, const point &r)
    {
    return l.y < r.y || (l.y == r.y && l.x < r.x);
    }

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

    int graham()
    {
    int i, len, top = 1;
    sort(pnt, pnt
    + n);
    if (n == 0) return 0; res[0] = pnt[0];
    if (n == 1) return 1; res[1] = pnt[1];
    if (n == 2) return 2; res[2] = pnt[2];
    for (i = 2; i < n; i++)
    {
    while (top && mult(pnt[i], res[top], res[top-1]))
    top
    --;
    res[
    ++top] = pnt[i];
    }
    len
    = top; res[++top] = pnt[n - 2];
    for (i = n - 3; i >= 0; i--)
    {
    while (top!=len && mult(pnt[i], res[top], res[top-1])) top--;
    res[
    ++top] = pnt[i];
    }
    return top; // 返回凸包中点的个数
    }

    double dist(point p1,point p2)
    {
    return (sqrt((p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y)));
    }

    double work()
    {
    double ret = dist(res[m - 1], res[0]);
    for (int i = 0; i < m - 1; i++)
    ret
    += dist(res[i], res[i + 1]);
    if (n == 2)
    return ret / 2;
    return ret;
    }

    int main()
    {
    //freopen("D:\\t.txt", "r", stdin);
    while (scanf("%d", &n) != EOF && n != 0)
    {
    input();
    m
    = graham();
    printf(
    "%.2f\n", work());
    }
    return 0;
    }
  • 相关阅读:
    Struts2_day01--导入源文件_Struts2的执行过程_查看源代码
    R语言低级绘图函数-text
    R语言低级绘图函数-rect
    R语言低级绘图函数-arrows
    R语言低级绘图函数-abline
    R语言绘图布局
    find_circ 识别circRNA 的原理
    CIRI 识别circRNA的原理
    circRNA 序列提取中的难点
    tRNA 二级结构预测可视化
  • 原文地址:https://www.cnblogs.com/rainydays/p/1999202.html
Copyright © 2011-2022 走看看