欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Wall(poj 1113) //凸包应用

程序员文章站 2022-03-02 10:21:36
...

http://poj.org/problem?id=1113
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。
解析: 很容易想到求根据这n个点求凸包周长,又因为围墙城堡间要有距离,所以需要再建L为半径周长的围墙。(画个图会很明晰)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define ft first
#define sd second
#define faster ios::sync_with_stdio(0),cin.tie(0)
const int maxn=1000+10;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const double eps=1e-8;
int n,m;
int sgn(double x){
    if(fabs(x)<eps)return 0;
    if(x<0)return -1;
    else return 1;
}
struct point{
    double x,y;
    point(){}
    void input(){
        cin>>x>>y;
    }
    point(double x,double y):x(x),y(y){}
    bool operator ==(point b)const {
        return sgn(x-b.x)==0&&sgn(y-b.y)==0;
    }
    bool operator <(point b)const {
        return sgn(x-b.x)==0?sgn(y-b.y)<0:x<b.x;
    }
    point operator -(const point &b)const {
        return point(x-b.x,y-b.y);
    }
    double operator ^(const point &b)const {
        return x*b.y-y*b.x;
    }
    double distance(point p){
        return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
    }
};
struct line{
    point s,e;
    line(){}
    line(point s,point e):s(s),e(e){}
};
struct polygon{
    int n;
    point p[maxn];
    line l[maxn];
    void input(int _n){
        n=_n;
        for(int i=0;i<n;i++)p[i].input();
    }
    void getline(){
        for(int i=0;i<n;i++){
            l[i]=line(p[i],p[(i+1)%n]);
        }
    }
    struct cmp{
        point p;
        cmp(const point &p0){p=p0;}
        bool operator()(const point &aa,const point &bb){
            point a=aa,b=bb;
            int d=sgn((a-p)^(b-p));
            if(d==0){
                return sgn(a.distance(p)-b.distance(p)<0);
            }
            return d>0;
        }
    };
    void norm(){
        point mi=p[0];
        for(int i=1;i<n;i++)mi=min(mi,p[i]);
        sort(p,p+n,cmp(mi));
    }
    void getconvex(polygon &c){
        sort(p,p+n);
        c.n=n;
        for(int i=0;i<min(n,2);i++){
            c.p[i]=p[i];
        }
        if(c.n==2&&(c.p[0]==c.p[1]))c.n--;
        if(n<=2)return;
        int &top=c.n;
        top=1;
        for(int i=2;i<n;i++){
            while(top&&sgn((c.p[top]-p[i])^(c.p[top-1]-p[i]))<=0)top--;
            c.p[++top]=p[i];
        }
        int temp=top;
        c.p[++top]=p[n-2];
        for(int i=n-3;i>=0;i--){
            while(top!=temp&&sgn((c.p[top]-p[i])^(c.p[top-1]-p[i]))<=0)top--;
            c.p[++top]=p[i];
        }
        if(c.n==2&&(c.p[0]==c.p[1]))c.n--;
        c.norm();
    }
    double getcir(){
        double sum=0;
        for(int i=0;i<n;i++)sum+=p[i].distance(p[(i+1)%n]);
        return sum;
    }
};
int main()
{
    faster;
    int n;double m;
    cin>>n>>m;
    double ans=2.0*pi*m;
    polygon p,c;
    p.input(n);
    p.getline();
    p.getconvex(c);
    ans+=c.getcir();
    printf("%.0f\n",ans);
    return 0;
}
相关标签: 几何