zoukankan      html  css  js  c++  java
  • POJ 1113:Wall(凸包)

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

    Wall
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 34616   Accepted: 11821

    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

    结果四舍五入就可以了
     
    丢一个kuangbin的凸包模板。。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <iostream>
     6 using namespace std;
     7 #define N 1005
     8 const double PI = acos(-1.0);
     9 
    10 struct point
    11 {
    12     int x,y;
    13 }p[N];
    14 
    15 int stak[N],top, n, L;
    16 
    17 int cross(point p0, point p1, point p2)
    18 {
    19     return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
    20 }//计算叉积 p0p1 x p0p2
    21 
    22 double dis(point p1, point p2)
    23 {
    24     return sqrt( (double)(p2.x - p1.x) * (p2.x - p1.x) + (double)(p2.y - p1.y) * (p2.y - p1.y) );
    25 }//计算距离 p1p2的距离
    26 
    27 bool cmp(point p1, point p2)
    28 {
    29     int tmp = cross(p[0], p1, p2);
    30     if(tmp > 0) return true;
    31     else if( tmp == 0 && dis(p[0], p1) < dis(p[0], p2) ) return true;
    32     return false;
    33 }//极角排序函数,角度相同则距离小的在前面
    34 
    35 void init()
    36 {
    37     point pp;
    38     scanf("%d%d", &p[0].x, &p[0].y);
    39     pp.x = p[0].x, pp.y = p[0].y;
    40     int tmp = 0;
    41     for(int i = 1; i < n; i++) {
    42         scanf("%d%d", &p[i].x, &p[i].y);
    43         if( (pp.y > p[i].y) || (pp.y == p[i].y) && (pp.x > p[i].x) ) {
    44             tmp = i;
    45             pp = p[i];
    46         }
    47     }
    48     p[tmp] = p[0];
    49     p[0] = pp;
    50     sort(p+1, p+n, cmp);
    51 }//输入函数并进行预处理,将所有的点输入, 在最左下方的点放到p[0], 并进行极角排序
    52 
    53 void Graham()
    54 {
    55     if(n <= 1) {
    56         top = 0;
    57         stak[0] = 0;
    58     }
    59     else if(n == 2) {
    60         top = 1;
    61         stak[0] = 0;
    62         stak[1] = 1;
    63     }
    64     else{
    65         stak[0] = 0;
    66         stak[1] = 1;
    67         top = 1;
    68         for(int i = 2; i < n; i++) {
    69             while( top > 0 && cross(p[stak[top-1]], p[stak[top]], p[i]) <= 0)
    70                 top--;
    71             top++;
    72             stak[top] = i;
    73         }
    74     }
    75 }//凸包Graham模板
    76 
    77 int main()
    78 {
    79     while(~scanf("%d%d",&n,&L)) {
    80         init();
    81         Graham();
    82 
    83         //本题求的是凸包的周长 + 一个圆的周长
    84         double res = 0;
    85         for(int i = 0; i < top; i++) {
    86             res += dis(p[stak[i]], p[stak[i+1]]);
    87         }
    88         res += dis(p[stak[0]], p[stak[top]]);
    89         res += 2 * L * PI;
    90 
    91         printf("%.0f
    ", res);
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    快速排序
    Web总结
    浏览器兼容性问题汇总
    AngularJS理论基础
    预处器的对比——Sass、LESS.
    js事件知识整理
    Java script基础
    重拾nodeJs
    全国城市三级联动
    js 获取地址栏参数
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5680668.html
Copyright © 2011-2022 走看看