您的位置:首页 > 其它

POJ 1113 Wall 凸包

2017-08-05 10:48 253 查看
凸包加上圆周长即为结果,用了kuangbin的凸包模板

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;

const int MAXN=1000;
const double PI = acos(-1);

struct Point
{
int x,y;
};

Point pot[MAXN];
int stck[MAXN],top;

int cross(Point p0,Point p1,Point p2) //计算叉积  p0p1 X p0p2
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}

double dis(Point p1,Point p2)  //计算 p1p2的 距离
{
return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}

bool cmp(Point p1,Point p2) //极角排序函数,角度相同则距离小的在前面
{
int tmp=cross(pot[0],p1,p2);
if(tmp>0) return true;
else if(tmp==0&&dis(pot[0],p1)<dis(pot[0],p2)) return true;
else return false;
}

void init(int n) //输入,并把最左下方的点放在 pot[0]。并且进行极角排序
{
int i,k;
Point p0;
p0 = pot[0];
k=0;
for(i=1; i<n; i++)
{
if( (p0.y>pot[i].y) || ((p0.y==pot[i].y)&&(p0.x>pot[i].x)) )
{
p0 = pot[i];
k=i;
}
}
pot[k]=pot[0];
pot[0]=p0;
sort(pot+1,pot+n,cmp);
}

void graham(int n)
{
int i;
if(n==1)
{
top=0;
stck[0]=0;
}
if(n==2)
{
top=1;
stck[0]=0;
stck[1]=1;
}
if(n>2)
{
for(i=0; i<=1; i++) stck[i]=i;
top=1;

for(i=2; i<n; i++)
{
while(top>0&&cross(pot[stck[top-1]],pot[stck[top]],pot[i])<=0) top--;
top++;
stck[top]=i;
}
}
}

int main()
{
//    freopen("in.txt","r",stdin);
int n,l;
while(~scanf("%d%d", &n, &l))
{
for(int i=0;i<n;i++)
scanf("%d%d", &pot[i].x, &pot[i].y);
init(n);
graham(n);
double ans = 0;
for(int i=0;i<=top;i++)
ans += dis(pot[stck[i]], pot[stck[(i+1)%(top+1)]]);
ans += PI*l*2;
printf("%d\n", (int)(ans+0.5));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: