zoukankan      html  css  js  c++  java
  • hdu1348

    Wall

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3257    Accepted Submission(s): 931


    Problem 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.

    This problem contains multiple test cases!

    The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

    The output format consists of N output blocks. There is a blank line between output blocks.
     
    Sample Input
    1
    9 100
    200 400
    300 400
    300 300
    400 300
    400 400
    500 400
    500 200
    350 200
    200 200
     
    Sample Output
    1628
     
    题意:要在城堡外面建立城墙,要求城堡任意点的距离与城墙的距离>=r,求所建城墙的周长最短。
    思路:该题是凸包问题,墙的周长 = 凸包的周长 + 以r为半径的圆的周长。
     
     1 #include<algorithm>
     2 #include<stdio.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<math.h>
     6 #include<queue>
     7 #include<map>
     8 #include<iostream>
     9 using namespace std;
    10 #define N 1010
    11 const double PI=acos(-1.0);
    12 struct point
    13 {
    14     double x,y,angel;
    15 } p[N],stack[N];
    16 int top,n;
    17 
    18 double dis(point a,point b)//求距离
    19 {
    20     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    21 }
    22 
    23 bool mult(point p1,point p2,point p0)//叉乘
    24 {
    25     return (p1.x-p0.x)*(p2.y-p0.y) >= (p2.x-p0.x)*(p1.y-p0.y);
    26 }
    27 
    28 bool cmp(point a,point b)
    29 {
    30     if(a.angel == b.angel)
    31     {
    32         if (a.x == b.x)
    33             return a.y > b.y;
    34         return a.x > b.x;
    35     }
    36     return a.angel < b.angel;
    37 }
    38 
    39 void graham()
    40 {
    41 //p为点集,n为点的个数,stack为凸包点集,top为凸包个数
    42     int i,k=0;
    43     point temp;
    44     for(i=0; i<n; i++)
    45         if(p[i].y<p[k].y||((p[i].y==p[k].y)&&(p[i].x<p[k].x)))
    46             k=i;
    47     temp=p[0];
    48     p[0]=p[k];
    49     p[k]=temp;
    50     for(i=1; i<n; i++)
    51         p[i].angel=atan2(p[i].y-p[0].y,p[i].x-p[0].x);
    52     sort(p+1,p+n,cmp);
    53     stack[0]=p[0];
    54     stack[1]=p[1];
    55     stack[2]=p[2];
    56     top=3;
    57     for(i=3; i<n; i++)
    58     {
    59         while(top > 2 && mult(stack[top-2],stack[top-1],p[i])<=0)
    60             top--;
    61         stack[top++]=p[i];
    62     }
    63 }
    64 
    65 int main()
    66 {
    67     int T,i,flag=0;
    68     double r,ans;
    69     scanf("%d",&T);
    70     while(T--)
    71     {
    72         scanf("%d%lf",&n,&r);
    73         for(i=0; i<n; i++)
    74             scanf("%lf%lf",&p[i].x,&p[i].y);
    75         graham();
    76         ans=0;
    77         for(i=0; i<top-1; i++)
    78             ans+=dis(stack[i],stack[i+1]);
    79         ans+=dis(stack[top-1],stack[0]);
    80         ans+=2*PI*r;
    81         if(flag==1)
    82             printf("
    ");
    83         printf("%.0lf
    ",ans);
    84         flag=1;
    85     }
    86     return 0;
    87 }
    View Code
  • 相关阅读:
    windows下编译及使用libevent
    安装和使用memcached
    BroadcastReceiver插件化解决方案
    Service插件化解决方案
    Activity插件化解决方案
    换肤-插件化
    资源的插件化
    startActivity进行Hook
    代理模式
    对反射的封装
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3899446.html
Copyright © 2011-2022 走看看