首页 > 代码库 > POJ1113 Wall【凸包】

POJ1113 Wall【凸包】

                                                                                                  Wall

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24604   Accepted: 8183

Description

King想在自己的n个城堡外建Wall,使Wall与任一城堡距离至少为L且能围住它的城堡. 
技术分享

求Wall最短长度. 

Input

第一行为 N (3 <= N <= 1000) 和 L(1 <= L <= 1000).
接下来 N 行,每行为城堡坐标Xi,Yi (-10000 <= Xi, Yi <= 10000).

Output

输出一行Wall最短长度.

Sample Input

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

Sample Output

1628

Hint

结果四舍五入就可以了

题解:凸包的模板题

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define Max 1005
 6 using namespace std;
 7 const double pi=acos(-1.0);
 8 int n,l,top;//top记录栈顶
 9 double ans; 
10 struct point{
11     double x,y;
12     point(double X=0,double Y=0){x=X,y=Y;}
13     friend bool operator <(point a,point b){
14         return a.x<b.x||(a.x==b.x&&a.y<b.y);//xy排序 
15     }
16 }p[Max],que[Max];//que保存凸包节点 栈
17 point operator +(point a,point b){return point(a.x+b.x,a.y+b.y);}
18 point operator -(point a,point b){return point(a.x-b.x,a.y-b.y);}
19 double cross(point a,point b){return a.x*b.y-a.y*b.x;} //若<=0 说明b在a的顺时针方向 
20 double len(point a){return sqrt(a.x*a.x+a.y*a.y);}
21 int main()
22 {
23     scanf("%d%d",&n,&l);
24     for(int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
25     sort(p+1,p+n+1);
26     for(int i=1;i<=n;i++){//下凸壳 
27         while(top>1&&cross(que[top]-que[top-1],p[i]-que[top-1])<=0)top--;
28         que[++top]=p[i];
29     }
30     int tmp=top;
31     for(int i=n-1;i>=1;i--){//上凸壳 
32         while(top>tmp&&cross(que[top]-que[top-1],p[i]-que[top-1])<=0)top--;
33         que[++top]=p[i];
34     }
35     for(int i=1;i<=top;i++) ans+=len(que[i%top+1]-que[(i+1)%top+1]);
36     ans+=2*pi*l;
37     printf("%.0lf",ans);
38     return 0;
39 } 

 

 

POJ1113 Wall【凸包】