Wall POJ - 1113 凸包问题。(Andrew算法)

问题:

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

结果四舍五入就可以了

题意:给你n个点的坐标,再给你一个L,让你找到这n个点所能构成的最大凸边形(凸边形就是内角小于180°的多边形),然后在凸边形周围盖个以L为半径的圆墙,求圆墙的周长和凸边形的周长之和。

思路:用Andrew算法求凸边形的周长。最后加上圆墙的周长即可。

Andrew算法:

想想画个凸多边形是怎么画的。
首先最左边的点和最右边的点肯定要选吧。
假设把最左边的点作为起笔,那不就是绕一圈就好了吗?
对啊,这就是AndrewAndrew算法。
首先我们把笔尖定在左端点,然后往右画,在向左,最后回到原点。
画出来的轨迹类似一个圆。
类似GrahamGraham扫描法,我们同样要判断这个点能不能被选作为凸多边形的顶点。
然后到了最右端之后,我们再从右边往左扫。
回到最左端后,你就画了一个凸多边形了。
好了,重点来了!
怎么判断要不要选这个点呢?
也就是说怎么判断选的这些点是不是凸多边形的一部分?
想想顺着笔画,凸多边形的边都有哪些特点。
看看刚刚这个图

为什么连P1−P3P1−P3而不是把P1−P2,P2−P3P1−P2,P2−P3给连了?
很清晰了吧。
就是斜率嘛!
要是P2−P3P2−P3的斜率没有P1−P3P1−P3大,那它肯定是凹进去的。
那就没有用啦,踢掉!
然后一直找,来回一波,就找到了答案了。

下面是添加一些这道题的一些数学知识,数学太烂了:

向量的点乘:a* b    公式:a * b = |a| * |b| * cosθ      向量点乘公式a(x1,y1),b(x2,y2),则a×b=x1x2+y1y2,

向量的叉乘:a∧b   公式:a ∧ b = |a| * |b| * sinθ      向量叉乘公式a(x1,y1),b(x2,y2),则a×b=x1y2-x2y1,

叉乘判断凹凸边形的问题:例平面上有三个点:p1(x1,y1),p2(x2,y2),p3(x3,y3)

s(p1,p2,p3)=(x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)

如果s>0 则说明 这连接这3个点时是按照逆时针的顺序,如果是s<0则说明连接这3个点是按照顺时针的顺序(画一下三个点就明白了).

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<map>
#include<stdlib.h>
#include<iostream>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof(a))
#define N 2100
int n,L,num,cnt;
double sum;
struct node
{
    int x,y;
}q[N],p[N];
bool zmh(node a,node b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
int pan(node a,node b,node c)//向量叉乘
{return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);}
void Andrew()
{
    num=0;
    for(int i=0;i<n;i++)//从最左点开始逆时针跑到最右点,完成凸边形的上一半部分
    {
        while(num>1&&pan(p[num-2],p[num-1],q[i])<=0)num--;
        p[num++]=q[i];
    }
    cnt=num;
    for(int i=n-1;i>=0;i--)//从最右点开始逆时针跑到最左点,完成凸边形的下一半部分
    {
        while(num>cnt&&pan(p[num-2],p[num-1],q[i])<=0)num--;
        p[num++]=q[i];
    }
}
int main()
{
    while(~scanf("%d%d",&n,&L))
    {
        for(int i=0;i<n;i++)
            scanf("%d%d",&q[i].x,&q[i].y);
        sort(q,q+n,zmh);//从最左下方开始
        Andrew();
        sum=2.00*PI*L;
        for(int i=1;i<num;i++)
        sum+=sqrt((p[i].x-p[i-1].x)*(p[i].x-p[i-1].x)+(p[i].y-p[i-1].y)*(p[i].y-p[i-1].y));
        //满足条件的点与点之间的距离加在一起
        int ans=sum+0.5;//四舍五入,也可用%.0lf输出也是四舍五入的意思
        printf("%d\n",ans);
    }
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值