[hdu 1348] Wall · 凸包

来源:互联网 发布:电影翻译软件 编辑:程序博客网 时间:2024/04/30 04:05

大概就是,给你n个点和一个值L,求这n个点的凸包,然后求与凸包相距l的外圈的周长。

结果=凸包的周长+半径的L的圆的周长

求凸包:

首先将n个按x坐标排序,然后选第一个点作为一个确定的点(最左边的点肯定在凸包上)

然后分两步,先求上凸包,再求下凸包。

求上凸包时,我们将目前已经在凸包里的点压入栈中,当做到第i号点的时候,如果发现

向量<top-1,i>在向量<top-1,top>的逆时针方向,那么说明top是凸包内的点而不是凸包上的点(现在求的是上凸包),所以将top从栈中弹出。

求下凸包的时候可以当成翻过来求上凸包,从后往前再扫一遍就行啦。


判断两个向量的相对方向可以用叉积,设有两个向量A(x1,y1),B(x2,y2),则A×B=x1y2-x2y1,

若B在A的逆时针方向,则A×B>0;

若B在A的顺时针方向,则A×B<0。


#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;#define pi 3.1415926535#define sqr(x) ((x)*(x))const int N=1005;struct arr{int x,y;}a[N];int stack[N],top;int T,n,l;double ans;bool cmp(const arr A,const arr B){return A.x<B.x || A.x==B.x && A.y<B.y;} double cha(int i,int j,int k){int x1,x2,y1,y2;x1=a[j].x-a[i].x;y1=a[j].y-a[i].y;x2=a[k].x-a[i].x;y2=a[k].y-a[i].y;return x1*y2-x2*y1;}void CC(){top=0;stack[++top]=1;//求上凸包 for (int i=2;i<=n;i++){while (top>1 && cha(stack[top-1],stack[top],i)>=0) top--;stack[++top]=i;}int tmp=top;//求下凸包  for (int i=n-1;i>=1;i--){while (top>tmp && cha(stack[top-1],stack[top],i)>=0) top--;stack[++top]=i;}ans=2*pi*l;for (int i=1;i<top;i++){int p,q;p=stack[i];q=stack[i+1];ans+=sqrt(sqr(a[p].x-a[q].x)+sqr(a[p].y-a[q].y));}ans+=sqrt(sqr(a[stack[top]].x-a[1].x)+sqr(a[stack[top]].y-a[1].y));}int main(){scanf("%d",&T);while (T--){scanf("%d%d",&n,&l);for (int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);sort(a+1,a+n+1,cmp);CC();printf("%.0lf\n",ans);if (T) puts("");//注意格式。。。。。囧 }return 0;}



0 0
原创粉丝点击