LightOj1203 - Guarding Bananas(凸包求多边形中的最小角)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1203

题意:给你一个点集,求凸包中最小的角;模板题,但是刚开始的时候模板带错了,错的我都想吐了;

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
const double eps = 1e-;
const double PI = acos(-);
const int N = ; struct point
{
double x, y;
point(double x=, double y=) : x(x), y(y){}
friend point operator - (const point& p1, const point& p2)
{
return point(p1.x-p2.x, p1.y-p2.y);
}
friend double operator ^ (const point& p1, const point& p2)
{
return p1.x*p2.y - p1.y*p2.x;
}
}p[N], res[N];
double Dist(point p1, point p2)
{
double dx = p1.x - p2.x, dy = p1.y - p2.y;
return sqrt(dx*dx + dy*dy);
}
bool cmp1(point p1, point p2)
{
if(p1.y == p2.y)
return p1.x < p2.x;
return p1.y < p2.y;
}
bool cmp2(point p1, point p2)///极角排序;若极角相同,距离近的在前面;
{
double k = (p1-p[])^(p2-p[]);
if( k>eps || (fabs(k)<eps && Dist(p1, p[]) < Dist(p2, p[]) ))
return ;
return ;
}
int Graham(int n)
{
res[] = p[];if(n == ) return ;
res[] = p[];if(n == ) return ;
int top = ;
for(int i=; i<n; i++)
{
while(top && ((res[top]-res[top-])^(p[i]-res[top-])) <= ) top--;
res[++top] = p[i];
}
return top+;
}
int main()
{
int n, T, tCase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
sort(p, p+n, cmp1);///p[0]为最下方靠左的点;
sort(p+, p+n, cmp2);///以p[0]为基点,按叉积进行排序;
int cnt = Graham(n);///求凸包的顶点个数cnt,保存在res中,下标从0开始;
if(cnt < )
{
printf("Case %d: 0\n", tCase++);
continue;
}
double ans = ;
res[cnt] = res[], res[cnt+] = res[];
for(int i=; i<=cnt; i++)
{
double a = Dist(res[i-], res[i+]);
double b = Dist(res[i-], res[i]);
double c = Dist(res[i], res[i+]);
ans = min(ans, acos((b*b+c*c-a*a)/(*b*c)));
}
printf("Case %d: %.6f\n", tCase++, ans*/PI);
}
return ;
}
上一篇:java创建TXT文件并进行读、写、修改操作


下一篇:PHP cURL获取微信公众号access_token