zoukankan      html  css  js  c++  java
  • Wall(凸包)

    http://poj.org/problem?id=1113

    题意:给出一些点的坐标,和一个半径r,求出这些点围成的凸包的周长再加上一个半径为r的圆的周长。

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <math.h>
     4 const double PI=acos(-1.0);
     5 const int N=1002;
     6 using namespace std;
     7 
     8 struct Point
     9 {
    10     double x;
    11     double y;
    12     Point (double x = 0,double y = 0):x(x),y(y) {}
    13     bool friend operator < (const Point &a, const Point &b)
    14     {
    15         return a.x < b.x||(a.x==b.x&&a.y < b.y);
    16     }
    17 } p[N],c[N];
    18 typedef Point Vector;
    19 double dis(Point A,Point B)//两点间的距离
    20 {
    21     return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
    22 }
    23 Vector operator- (Point A,Point B)//求向量
    24 {
    25     return Vector(B.x-A.x,B.y-A.y);
    26 }
    27 double Cross(Vector A,Vector B)//求叉积
    28 {
    29     return A.x*B.y-A.y*B.x;
    30 }
    31 int ConvexHull(Point *p,int n,Point *c)//计算凸包
    32 {
    33     int k = 0;
    34     for (int i = 0; i < n; i++)
    35     {
    36         while(k > 1 && Cross(c[k-2]-c[k-1],c[k-2]-p[i])<=0)
    37             k--;
    38         c[k++] = p[i];
    39 
    40     }
    41     int m = k;
    42     for (int i = n-2; i >= 0; i--)
    43     {
    44         while(k > m && Cross(c[k-2]-c[k-1],c[k-2]-p[i])<=0)
    45             k--;
    46         c[k++] = p[i];
    47     }
    48     c[k] = c[0];
    49     return k;
    50 }
    51 
    52 int main()
    53 {
    54     int n;
    55     double r;
    56     scanf("%d %lf",&n,&r);
    57     for (int i = 0; i < n; i++)
    58         scanf("%lf %lf",&p[i].x,&p[i].y);
    59     sort(p,p+n);
    60     int cnt = ConvexHull(p,n,c);
    61     double len = 0;
    62     for (int i = 1; i <= cnt; i++)
    63     {
    64         len+=dis(c[i],c[i-1]);
    65     }
    66     len+=2*PI*r;
    67     printf("%.0f",len);
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    Linux 字典数组应用
    Linux shell 字符串切割 内置方法
    【Swing/文本组件】定义自动换行的文本域
    【C++语法基础】实验1
    【Swing程序设计/常用面板】
    【标签组件与图标 3.3】
    【2018.2.26算法总结#分治】
    数据结构#课表排序及查询
    数据结构#前序遍历建立二叉树 输出中序遍历
    OJ#1002 又是a+b
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3388586.html
Copyright © 2011-2022 走看看