poj 1113 Wall(凸包)

本文提供了一种解决POJ 1113问题的方法,该问题要求计算围绕给定多边形城堡并保持一定距离的围墙最小长度。通过构造城堡顶点的凸包和增加额外的圆形边界来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,并且墙与所有点的距离至少为L,求这个墙最小的长度。

 

公式:

城堡围墙长度最小值 = 城堡顶点坐标构成的散点集的凸包总边长 + 半径为L的圆周长

View Code
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define PI 3.1415926535
 6 using namespace std;
 7 const double eps=1e-6;
 8 typedef struct node
 9 {
10     int x,y;
11 }point;
12 int n;
13 point pt[1100];
14 point res[1100];
15 bool cmp(point a,point b)
16 {
17     if(a.x==b.x)
18     return a.y<b.y;
19     else
20     return a.x<b.x;
21 }
22 int dcml(double x)
23 {
24     if(fabs(x)<eps)
25     return 0;
26     if(x<0)
27     return -1;
28     else
29     return 1;
30 }
31 double dot(int x1,int y1,int x2,int y2)
32 {
33     return x1*y2-x2*y1;
34 }
35 int cross(point a,point b,point c)
36 {
37     return dcml(dot(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y));
38 }
39 int dist(point a,point b)
40 {
41     return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
42 }
43 int main()
44 {
45     int L;
46     scanf("%d%d",&n,&L);
47     int i;
48     for(i=0;i<n;i++)
49     {
50         scanf("%d%d",&pt[i].x,&pt[i].y);
51     }
52     sort(pt,pt+n,cmp);
53     int m;
54     m=0;
55     for(i=0;i<n;i++)
56     {
57         while(m>1&&cross(res[m-1],pt[i],res[m-2])<=0)
58         m--;
59         res[m++]=pt[i];
60     }
61     int k=m;
62     for(i=n-2;i>=0;i--)
63     {
64         while(m>k&&cross(res[m-1],pt[i],res[m-2])<=0)
65         m--;
66         res[m++]=pt[i];
67     }
68     if(n>1)
69     m--;
70     double dis=0;
71     for(i=0;i<m-1;i++)
72     {
73         dis+=sqrt(dist(res[i],res[i+1]));
74     }
75     dis+=sqrt(dist(res[0],res[m-1]));
76     dis+=2*PI*L;
77     printf("%d",(int)(dis+0.5));
78     return 0;
79 }

 

转载于:https://www.cnblogs.com/wanglin2011/archive/2013/03/08/2950381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值