码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1113 Wall (凸包)

时间:2014-07-16 08:53:00      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:poj   算法   

Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29116   Accepted: 9744

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. 
bubuko.com,布布扣

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


题意:给出平面上若干个点的坐标,让建一个环形围墙,把所有的点围在里面,且距所有点的距离不小于l。求围墙的最小长度。

分析:凸包周长+半径为l的圆周长。

  • Source Code
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <math.h>    
    using namespace std;
    #define M 1000
    int top;
    struct node {
    	double x,y; 
    }p[M],stack[M];
    //计算两点间距离。
    double L(node a,node b)
    {
    	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    //计算叉积的大小。
    double multi(node a,node b,node c)
    {
    	return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
    }
    //计算左转还是右转,大于零就是左转,小于零就是右转。
    int cmp(node a,node b)
    {   
    	if(multi(a,b,p[0])>0)
    		return 1;
    	if(multi(a,b,p[0])==0&&L(a,p[0])<L(b,p[0]))
    		return 1;
    	return 0;
    }
    //核心部分
    void GS(node p[],node stack[],int n)
    { 
    	int i,k=0;
    	node temp;
    	for(i=0;i<n;i++)
    	{
    		if(p[i].y<p[k].y||((p[i].y==p[k].y)&&(p[i].x<p[k].x)))
    			k=i;
    	}
    	temp=p[0];
    	p[0]=p[k];
    	p[k]=temp;
    	sort(p+1,p+n,cmp); //p[0]已经是最小,没必要再对它进行排序。
    	top=2;             //开始时栈里已经有3个元素。
    	stack[0]=p[0],stack[1]=p[1],stack[2]=p[2];
    	for(i=3;i<n;i++)
    	{  
    		while(top>1&&multi(p[i],stack[top],stack[top-1])>=0)
    			top--;           //如果右转,栈顶元素出栈。
    		stack[++top]=p[i];       //找到左转,元素入栈。
    	}
    } 
    int main()
    {   
    	double t;
    	int n,i,m;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{   t=0;
    	memset(p,0,sizeof(p));
    	memset(stack,0,sizeof(stack));
    	for(i=0;i<n;i++)
    	{ 
    		scanf("%lf %lf",&p[i].x,&p[i].y);  //有小数,用double} 
    	GS(p,stack,n);
    	stack[top+1]=stack[0];                //最后一个和第一个也有距离要加。
    	for(i=0;i<=top;i++)
    	{
    		t+=L(stack[i],stack[i+1]);
    	}
    	   t=t+2*m*3.1415926;            
           printf("%.0lf\n",t);
    	}
    	return 0;
    }

POJ 1113 Wall (凸包),布布扣,bubuko.com

POJ 1113 Wall (凸包)

标签:poj   算法   

原文地址:http://blog.csdn.net/qq2256420822/article/details/37728247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!