Poj 1113 Wall (凸包Graham水平序)

╰+哭是因爲堅強的太久メ 2023-01-20 11:57 121阅读 0赞

以前做过的题,现在重新写下。

以前的解题报告:http://blog.csdn.net/whyorwhnt/article/details/8316442

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. const double PI=acos(-1.0);
  7. const double eps=1e-8;
  8. const int N=1005;
  9. struct Point //点,向量
  10. {
  11. double x,y;
  12. Point(){}
  13. Point(double _x,double _y)
  14. {x=_x;y=_y;}
  15. void Get ()
  16. {scanf("%lf%lf",&x,&y);}
  17. Point operator-(const Point &b)const
  18. {return Point(x-b.x,y-b.y);}
  19. Point operator+(const Point &b)
  20. {return Point(x+b.x,y+b.y);}
  21. Point operator*(const double k) //数乘
  22. {return Point(x*k,y*k);}
  23. double operator*(const Point a) //点乘
  24. {return x*a.x+y*a.y;}
  25. double operator^(const Point a) //叉乘
  26. {return x*a.y-y*a.x;}
  27. int DB (double dx) //判0函数
  28. {
  29. if(fabs(dx)<eps) return 0;
  30. return dx>0?1:-1;
  31. }
  32. //调用点a的该函数
  33. //返回1点a在向量bc的左侧
  34. //返回-1点a在向量bc的右侧
  35. //返回0点a在向量bc这条直线上
  36. int Cross (Point b,Point c)
  37. {return DB((b.x-x)*(c.y-y)-(c.x-x)*(b.y-y));}
  38. bool operator < (const Point &b)const //用于排序
  39. {
  40. if (fabs(y-b.y)<eps) return x-b.x<0;
  41. return y-b.y<0;
  42. }
  43. double Dis (Point ch)
  44. {return sqrt((x-ch.x)*(x-ch.x)+(y-ch.y)*(y-ch.y));}
  45. }pt[N],ch[N];
  46. int n,len;
  47. //求凸包函数
  48. //pt:所有的点,n个,pt[0]到pt[n-1]
  49. //ch:求完的凸包中的点,len个,q[0]到q[len-1]
  50. void Graham (Point pt[],Point ch[],int &len,int n) //只保存凸包顶点
  51. {
  52. int i,top=1;
  53. sort(pt,pt+n);
  54. ch[0]=pt[0];
  55. ch[1]=pt[1];
  56. for (i=2;i<n;i++)
  57. {
  58. while (top>0 && ch[top-1].Cross(ch[top],pt[i]) <= 0)
  59. top--;
  60. ch[++top]=pt[i];
  61. }
  62. int temp=top;
  63. for (i=n-2;i>=0;i--)
  64. {
  65. while (top>temp && ch[top-1].Cross(ch[top],pt[i]) <= 0)
  66. top--;
  67. ch[++top]=pt[i];
  68. }
  69. len=top;
  70. }
  71. int main ()
  72. {
  73. int L,i,n,len;
  74. double ans=0;
  75. scanf("%d%d",&n,&L);
  76. for (i=0;i<n;i++)
  77. pt[i].Get();
  78. Graham (pt,ch,len,n);
  79. for (i=0;i<len;i++)
  80. ans+=ch[i].Dis(ch[(i+1)%len]);
  81. ans+=PI*2*L;
  82. printf("%.0lf\n",ans);
  83. return 0;
  84. }

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读