【计算几何/凸包】POJ 1113 Wall

Wall

Time Limit: 1000MS Memory Limit: 10000K

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

Northeastern Europe 2001

题意

给你一个点集,求这个点集 N 米位置的凸包,对于转折点用圆弧代替。这里写图片描述
意思就是求上述图片橙色的线段
其中那个圆的半径也未N,直线离城墙的距离也为N。

思路

对于橙色线段的直线路径不难发现其就是凸包之后凸包点集的长度,而所有的曲线之和不难发现其就是一个以N为半径的圆形的周长。证明从略。
即 所有的长度等于凸包长度+以N为半径圆形周长。

扫描二维码关注公众号,回复: 2465808 查看本文章

坑点

弧形处理不好思考

AC代码

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const double PI = acos(-1);
class Point
{
public:
    double x,y;
    Point(double x = 0,double y = 0):x(x),y(y){}
    Point operator + (Point a){return Point(x + a.x , y + a.y);}
    Point operator - (Point b){return Point(x - b.x , y - b.y);}
    bool operator < (const Point &a) const
    {
        if(x==a.x)
            return y < a.y;
        return x < a.x;
    }
};

typedef vector<Point> Polygom;
typedef Point Vector;

double cross(Vector a,Vector b)
{
    return a.x*b.y - a.y*b.x;
}

bool isclock(Point p0,Point p1, Point p2)
{
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if(cross(a,b)< 0 ) return true;
    return false;
}

double getDistance(Point a,Point b)
{
    return sqrt(pow(a.x - b.x,2) + pow(a.y - b.y,2));
}

Polygom andrewScan(Polygom s)
{
    Polygom u,l;
    if(s.size() < 3) return s;
    sort(s.begin(),s.end());
    u.push_back(s[0]);
    u.push_back(s[1]);
    l.push_back(s[s.size()-1]);
    l.push_back(s[s.size()-2]);

    for(int i = 2 ; i < s.size() ; i++)
    {
        for(int n = u.size() ; n >= 2 && isclock(u[n-2],u[n-1],s[i])!=true ; n--)
            u.pop_back();
        u.push_back(s[i]);
    }

    for( int i = s.size() - 3 ; i >= 0 ; i--)
    {
        for(int n = l.size() ; n >= 2 && isclock(l[n-2],l[n-1],s[i])!= true ;n--)
            l.pop_back();
        l.push_back(s[i]);
    }
//    puts("this is u:");
//    for(auto &p : u) printf("%.2f %.2f\n",p.x,p.y);
//    puts("this is l:");
//    for(auto &p : l) printf("%.2f %.2f\n",p.x,p.y);

    for(int i = 1 ; i < l.size() - 1 ; i++) u.push_back(l[i]);
    return u;
}

void solve(void)
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        Polygom a;
        for(int i = 0 ; i < n  ; i ++)
        {
            Point t;
            scanf("%lf%lf",&t.x,&t.y);
            a.push_back(t);
        }
        double ans = 0;
        a = andrewScan(a);
        for(int i = 0 ; i < a.size() -1 ; i++)
        {
            ans += getDistance(a[i],a[i+1]);
        }
        ans+= getDistance(a[0],a[a.size()-1]);
        ans+=PI*2*k;
        ans = round(ans);
        printf("%.0f\n",ans);
    }
}

int main(void)
{
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/peng0614/article/details/81148236