zoukankan      html  css  js  c++  java
  • POJ 1113 Wall(计算几何の凸包)

    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.
     
    题目大意:要在一城堡外建一围墙,要求围墙与城堡的距离不能小于L,求围墙最小长度
    思路:求城堡凸包(完全没看出哪里要求围墙不能凹了……),再加上2*PI*L就是答案,围墙曲线的总长度就是2*PI*L
     
    Graham-Scan算法求凸包,不排极角的方法(防误差),32MS
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    
    struct POINT {
    	int x, y;
    	POINT(int xx = 0, int yy = 0): x(xx), y(yy) {}
    };
    
    const int MAXN = 1010;
    const double PI = acos(-1.0);
    
    int n, L;
    int stk[MAXN], top;
    POINT p[MAXN];
    
    inline bool cmp(const POINT &a, const POINT &b) {
    	if(a.y == b.y) return a.x < b.x;
    	return a.y < b.y;
    }
    //turn left
    inline bool Cross(POINT &sp, POINT &ep, POINT &op) {
    	return (sp.x - op.x) * (ep.y - op.y) - (ep.x - op.x) * (sp.y - op.y) >= 0;
    }
    
    inline double dist(POINT &a, POINT &b) {
    	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    
    void Graham_scan() {
    	std::sort(p, p + n, cmp);
    	top = 1;
    	stk[0] = 0; stk[1] = 1;
    	for(int i = 2; i < n; ++i) {
    		while(top && Cross(p[i], p[stk[top]], p[stk[top - 1]])) --top;
    		stk[++top] = i;
    	}
    	int len = top;
    	stk[++top] = n - 2;
    	for(int i = n - 3; i >= 0; --i) {
    		while(top != len && Cross(p[i], p[stk[top]], p[stk[top - 1]])) --top;
    		stk[++top] = i;
    	}
    }
    
    void solve() {
    	double ans = L * PI * 2;
    	stk[++top] = stk[0];
    	for(int i = 0; i < top; ++i)
    		ans += dist(p[stk[i]], p[stk[i+1]]);
    	printf("%.0f
    ", ans);
    }
    
    int main() {
    	while(scanf("%d%d", &n, &L) != EOF) {
    		for(int i = 0; i < n; ++i) scanf("%d%d", &p[i].x, &p[i].y);
    		Graham_scan();
    		solve();
    	}
    }
    

      

  • 相关阅读:
    第六章类(十一)构造函数4对象初始化器和集合初始化器
    第六章类(十)构造函数3new
    第六章类(九)构造函数2默认构造函数
    Javascript---- 练习八(字符串操作)
    图解CSS3----white-space空白符处理
    图解CSS3----word-break单词断行
    图解CSS3----word-wrap(单词换行)
    图解CSS3----text-overflow(文本溢出属性)
    图解CSS3----vertical-align(文本垂直对齐方式)
    Javascript---- 练习五(函数,事件绑定)
  • 原文地址:https://www.cnblogs.com/oyking/p/3193199.html
Copyright © 2011-2022 走看看