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();
    	}
    }
    

      

  • 相关阅读:
    Mysql查询正在运行的事务
    linux SVN添加新用户
    linux下安装php扩展amqp
    解决apache启动错误:Could not reliably determine the server's fully qualified domain name
    linux系统安装redis服务器与php redis扩展
    navicat导出数据库字典
    centos安装GD库失败
    Mysql实现主从同步
    计算机的本质
    windows下nginx访问web目录提示403 Forbidden
  • 原文地址:https://www.cnblogs.com/oyking/p/3193199.html
Copyright © 2011-2022 走看看