0
点赞
收藏
分享

微信扫一扫

poj1113 Wall --凸包


原题链接: ​​http://poj.org/problem?id=1113​​


一:分析



二:AC代码

#define _CRT_SECURE_NO_DEPRECATE 

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

struct Point
{
double x, y;

Point operator-(Point & p)
{
Point t;
t.x = x - p.x;
t.y = y - p.y;
return t;
}

double det(Point p)
{
return x*p.y - p.x*y;
}

double dist(Point & p)
{
return sqrt((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y));
}
};

bool cmp(Point & p1, Point & p2)
{
if (p1.x != p2.x)
return p1.x < p2.x;

return p1.y < p2.y;
}

Point point[1005];
int convex[1005];
int N, L;

int getConvexHull()
{
sort(point, point + N, cmp);
int temp;
int total = 0;

for (int i = 0; i < N; i++)
{
while (total > 1 && (point[convex[total - 1]] - point[convex[total - 2]]).det(point[i] - point[convex[total - 1]]) <= 0)
total--;
convex[total++] = i;
}

temp = total;

for (int i = N - 2; i >= 0; i--)
{
while (total > temp && (point[convex[total - 1]] - point[convex[total - 2]]).det(point[i] - point[convex[total - 1]]) <= 0)
total--;
convex[total++] = i;
}

return total;//返回组成凸包的点的个数,实际上多了一个,就是起点,所以组成凸包的点个数是total-1
}

int main()
{
scanf("%d%d", &N, &L);

for (int i = 0; i < N; i++)
scanf("%lf%lf", &point[i].x, &point[i].y);

int num = getConvexHull();
double ans = 0.0;

for (int i = 0; i < num - 1; i++)
ans += point[convex[i]].dist(point[convex[i + 1]]);

ans += 3.1415926 * 2 * L;

printf("%.0lf\n", ans);

return 0;
}








举报

相关推荐

0 条评论