• POJ1113 Wall —— 凸包


    题目链接:https://vjudge.net/problem/POJ-1113

    Wall
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 39281   Accepted: 13418

    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

    结果四舍五入就可以了

    Source

    题意:

    给出一座城堡(由点勾勒出来),问用最短城墙将城堡包围起来,且城墙与城堡的距离不能小于L。

    题解:

    1.假设没有规定城堡与城墙的距离,那么城墙的最短长度即为城墙点集的凸包的周长。

    2.再考虑回城墙与城堡的距离,那么就要把凸包的每条边往外垂直移动L距离。移动过后,每条边与相邻的边都没有了交点,这时,需要在两条边之间加一段圆弧,圆弧的半径即为L,而所有圆弧加起来就是一个完整的圆,为什么?如图:

    再根据初中知识:任意多边形的外角和为360度。即可得出结论。

    4.所以总长度为:凸包的周长+圆的周长。

    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int MOD = 1e9+7;
     17 const int MAXN = 1000+10;
     18 
     19 const double eps = 1e-8;
     20 const double PI = acos(-1.0);
     21 int sgn(double x)
     22 {
     23     if(fabs(x)<eps) return 0;
     24     if(x<0) return -1;
     25     else return 1;
     26 }
     27 
     28 struct Point
     29 {
     30     double x,y;
     31     Point(){}
     32     Point(double _x, double _y) {
     33         x = _x; y = _y;
     34     }
     35     Point operator -(const Point &b)const{
     36         return Point(x-b.x, y-b.y);
     37     }
     38     double operator *(const Point &b)const{
     39         return x*b.x+y*b.y;
     40     }
     41     double operator ^(const Point &b)const{
     42         return x*b.y-y*b.x;
     43     }
     44 };
     45 
     46 Point list[MAXN];
     47 int Stack[MAXN], top;
     48 
     49 double dis(Point a, Point b)
     50 {
     51     return sqrt((a-b)*(a-b));
     52 }
     53 
     54 bool cmp(Point p1, Point p2)
     55 {
     56     double tmp = (p1-list[0])^(p2-list[0]);
     57     if(sgn(tmp)>0) return true;
     58     else if(sgn(tmp)==0 && sgn(dis(p1,list[0])-dis(p2,list[0]))<=0)
     59         return true;
     60     else return false;
     61 }
     62 
     63 void Graham(int n)
     64 {
     65     Point p0;
     66     int k = 0;
     67     p0 = list[0];
     68     for(int i = 1; i<n; i++)
     69     {
     70         if(p0.y>list[i].y||(p0.y==list[i].y&&p0.x>list[i].x))
     71         {
     72             p0 = list[i];
     73             k = i;
     74         }
     75     }
     76     swap(list[k],list[0]);
     77     sort(list+1,list+n,cmp);
     78     if(n==1)
     79     {
     80         top = 1;
     81         Stack[0] = 0;
     82         return;
     83     }
     84     if(n==2)
     85     {
     86         top = 2;
     87         Stack[0] = 0;
     88         Stack[1] = 1;
     89         return;
     90     }
     91     Stack[0] = 0;
     92     Stack[1] = 1;
     93     top = 2;
     94     for(int i = 2; i<n; i++)
     95     {
     96         while(top>1 && sgn((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]]))<=0)
     97             top--;
     98         Stack[top++] = i;
     99     }
    100 }
    101 
    102 int main()
    103 {
    104     int L, n;
    105     while(scanf("%d%d", &n,&L)!=EOF)
    106     {
    107         for(int i = 0; i<n; i++)
    108             scanf("%lf%lf",&list[i].x,&list[i].y);
    109 
    110         Graham(n);
    111         double perimeter = 0;
    112         for(int i = 0; i<top; i++)
    113             perimeter += dis(list[Stack[i]],list[Stack[(i+1)%top]]);
    114         perimeter += 2.0*PI*L;
    115         printf("%.0f
    ", perimeter);
    116     }
    117 }
    View Code
  • 相关阅读:
    C++笔记--模板
    C++笔记--抽象机制
    C++--VECTOR的简单应用
    C++面试笔记--STL模板与容器
    C++面试笔记--指针和引用
    《转载》什么变量在堆内存里存放,什么变量在栈内存里存放
    C++面试笔记--const、sizeof
    C++面试笔记--宏定义
    C++笔记--指针数组和结构
    C++笔记--异常
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8585176.html
Copyright © 2020-2023  润新知