您的位置:首页 > 其它

poj 1113 Wall(Graham扫描法求凸包)

2012-04-11 23:48 483 查看
【题目大意】:给出一些点,求凸包周长,并加上给定圆周长

【解题思路】:极角排序,然后求凸包。模版testing

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>

using namespace std;

#define eps 1e-8
#define PI acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
#define INF 999999999.9

struct Point{
double x, y, dis;
}pt[1005],stack[1005],p0;
int top,tot;
int n;
double l;

//计算几何距离
double get_dis(double x1, double y1, double x2, double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

//极角比较, 返回-1: p0p1在p0p2的右侧,返回0:p0,p1,p2共线
int Cmp_PolarAngel(struct Point p1, struct Point p2, struct Point pb){
double delta=(p1.x-pb.x)*(p2.y-pb.y)-(p2.x-pb.x)*(p1.y-pb.y);
if (delta<0.0) return 1;
else if (delta==0.0) return 0;
else return -1;
}

// 判断向量p2p3是否对p1p2构成左旋
bool Is_LeftTurn(struct Point p3, struct Point p2, struct Point p1){
int type=Cmp_PolarAngel(p3, p1, p2);
if (type<0) return true;
return false;
}

//先按极角排,再按距离由小到大排
int Cmp(const void*p1, const void*p2){
struct Point*a1=(struct Point*)p1;
struct Point*a2=(struct Point*)p2;
int type=Cmp_PolarAngel(*a1, *a2, p0);
if (type<0) return -1;
else if (type==0){
if (a1->dis<a2->dis) return -1;
else if (a1->dis==a2->dis) return 0;
else return 1;
}
else return 1;
}

//求凸包
void Solve(int n){
int k;
p0.x=p0.y=INF;
for (int i=0; i<n; i++){
scanf("%lf %lf",&pt[i].x, &pt[i].y);
if (pt[i].y < p0.y){
p0.y=pt[i].y;
p0.x=pt[i].x;
k=i;
}
else if (pt[i].y==p0.y){
if (pt[i].x<p0.x){
p0.x=pt[i].x;
k=i;
}
}
}
pt[k]=pt[0];
pt[0]=p0;
for (int i=1; i<n; i++)
pt[i].dis=get_dis(pt[i].x,pt[i].y,p0.x,p0.y);
qsort(pt+1, n-1, sizeof(struct Point), Cmp);
//去掉极角相同的点
tot=1;
for (int i=2; i<n; i++)
if (Cmp_PolarAngel(pt[i], pt[i-1], p0))
pt[tot++]=pt[i-1];
pt[tot++]=pt[n-1];
//求凸包
top=1;
stack[0]=pt[0];
stack[1]=pt[1];
for (int i=2; i<tot; i++){
while (top>=1 && Is_LeftTurn(pt[i],stack[top],stack[top-1])==false)
top--;
stack[++top]=pt[i];
}
stack[top+1]=stack[0];
double ans=0;
ans=2.0*PI*(double)l;
for (int i=1; i<=top+1; i++){
ans+=get_dis(stack[i-1].x,stack[i-1].y,stack[i].x,stack[i].y);
}
printf("%.0f\n",ans);
}

int main(){
while (~scanf("%d%lf",&n,&l)){
Solve(n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: