【POJ1113】Wall(凸包)

【题目】

Description

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall. 

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements. 

The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle. 

Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

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

代码如下:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cmath>
 8 using namespace std;
 9 #define Maxn 1010
10 
11 struct Point
12 {
13     double x,y;
14     Point(double x=0,double y=0):x(x),y(y) {}
15 };
16 
17 typedef Point Vector;
18 Point p[Maxn];
19 
20 int n,l;
21 const double pi=3.1415926535898;
22 
23 double myabs(double x) {return x<0?-x:x;}
24 
25 const double eps=1e-10;
26 int dcmp(double x)//判断正负和零
27 {
28     if(myabs(x)<eps) return 0;
29     else return x<0?-1:1;
30 }
31 Vector operator - (Point A,Point B) {return Vector(A.x-B.x,A.y-B.y);}
32 
33 double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
34 double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
35 double Length(Vector A) {return sqrt(Dot(A,A));}
36 
37 
38 bool cmp(Point x,Point y) {return dcmp(x.x-y.x)==0?(x.y<y.y):(x.x<y.x);}
39 Point ch[Maxn];int len=0;
40 
41 void covexhull()
42 {
43     for(int i=1;i<=n;i++)
44     {
45         while(len>1&&Cross(ch[len]-ch[len-1],p[i]-ch[len-1])<=0) len--;
46         ch[++len]=p[i];
47     }int k=len;
48     for(int i=n-1;i>=1;i--)
49     {
50         while(len>k&&Cross(ch[len]-ch[len-1],p[i]-ch[len-1])<=0) len--;
51         ch[++len]=p[i];
52     }
53     if(n>1) len--;
54 }
55 
56 double PolygonLength()
57 {
58     double L=0;
59     for(int i=2;i<=len;i++)
60         L+=Length(ch[i]-ch[i-1]);
61     L+=Length(ch[1]-ch[len]);
62     return L;
63 }
64 
65 int main()
66 {
67     scanf("%d%d",&n,&l);
68     for(int i=1;i<=n;i++)
69     {
70         scanf("%lf%lf",&p[i].x,&p[i].y);
71     }
72     sort(p+1,p+1+n,cmp);
73     covexhull();
74     printf("%.0f\n",PolygonLength()+pi*l*2);
75     return 0;
76 }
[POJ1113]

 

2016-04-28 19:32:02



posted @ 2016-04-28 19:30  konjak魔芋  阅读(173)  评论(0编辑  收藏  举报