【HDOJ】1348 Wall

计算几何-凸包模板题目,Graham算法解。

 /* 1348 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std; #define MAXN 1005 typedef struct Point_t {
double x, y;
Point_t() {}
Point_t(double xx, double yy) {
x = xx; y = yy;
}
} Point_t; Point_t stack[MAXN];
Point_t points[MAXN];
const double PI = acos(-1.0);
int n; double Length(Point_t a, Point_t b) {
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
} double cross(Point_t p0, Point_t p1, Point_t p2) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
} bool comp(Point_t a, Point_t b) {
double k = cross(points[], a, b);
return !(k< || (k== && Length(points[], a)>Length(points[], b)));
} double Graham() {
double ret = ;
int i, j, k;
int top;
Point_t p0; p0 = points[];
k = ;
for (i=; i<n; ++i) {
if (points[i].x<p0.x || (points[i].x==p0.x && points[i].y<p0.y)) {
p0 = points[i];
k = i;
}
} points[k] = points[];
points[] = p0;
sort(points+, points+n, comp); stack[] = points[];
stack[] = points[];
stack[] = points[];
top = ;
for (i=; i<n; ++i) {
while (cross(stack[top-], stack[top], points[i])<= && top>)
--top;
stack[++top] = points[i];
}
stack[++top] = p0;
for (i=; i<=top; ++i)
ret += Length(stack[i-], stack[i]); return ret;
} int main() {
int t;
int i, j, k, tmp;
double l, ans; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif scanf("%d", &t);
while (t--) {
scanf("%d %lf", &n, &l);
for (i=; i<n; ++i)
scanf("%lf %lf", &points[i].x, &points[i].y);
ans = Graham();
ans += PI * (l+l);
printf("%.0lf\n", ans);
if (t)
printf("\n");
} return ;
}
上一篇:mysql学习笔记--数据库操作


下一篇:How to create QTP Shared Object Repository