POJ1113 Wall(凸包)

题目链接:

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

题目描述:

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

Hint

结果四舍五入就可以了

 

题目大意:

  有一些点,求一个闭曲线,闭曲线的边到每个点的距离都大于某个值

思路:

  求凸包,然后把凸包向外平移L个距离,然后在每个拐角加上弧线

  所以求的实际上就是凸包边长加上一个以L为半径的圆的周长

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int N = 1010;
 9 const double EPS = 1e-10;        //精度系数
10 const double PI = acos(-1.0);    //π
11 
12 struct Point {
13     double x, y;
14     Point(double x = 0, double y = 0) :x(x), y(y) {}
15     const bool operator < (Point A)const {
16         return x == A.x ? y < A.y : x < A.x;
17     }
18 };    //点的定义
19 
20 typedef Point Vector;    //向量的定义
21 
22 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }    //向量减法
23 
24 int dcmp(double x) {
25     if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1;
26 }    //与0的关系
27 
28 double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }    //向量点乘
29 double Length(Vector A) { return sqrt(Dot(A, A)); }    //向量长度
30 double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }    //向量叉乘
31 
32 int Convexhull(Point* p, int n, Point* ch) {
33     sort(p, p + n);
34     int m = 0;
35     for (int i = 0; i < n; ++i) {
36         while (m > 1 && dcmp(Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) --m;
37         ch[m++] = p[i];
38     }
39     int k = m;
40     for (int i = n - 2; i >= 0; --i) {
41         while (m > k && dcmp(Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) --m;
42         ch[m++] = p[i];
43     }
44     if (n > 1)--m;
45     return m;
46 }    //计算凸包 返回凸包顶点数 输出无重复点
47 
48 int main() {
49     int n, L;
50     scanf("%d%d", &n, &L);
51     Point p[N], ch[N];
52     for (int i = 0; i < n; ++i)
53         scanf("%lf%lf", &p[i].x, &p[i].y);
54     int m = Convexhull(p, n, ch);
55     double sum = 2 * PI * L;
56     for (int i = 0; i < m; ++i)
57         sum += Length(ch[(i + 1) % m] - ch[i]);
58     printf("%d\n", (int)(sum + 0.5));
59 }

 

Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37210   Accepted: 12685

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

Hint

结果四舍五入就可以了
posted @ 2017-06-29 13:38  hyp1231  阅读(130)  评论(0编辑  收藏  举报