POJ 1113 Wall 围墙 凸包

来源:互联网 发布:单片机最小系统组成 编辑:程序博客网 时间:2024/04/28 17:18

题意:给出一系列的点,在凸包外面l米建造围墙,求围墙的长度。

其实就是裸凸包+半径l的圆周长。

这题卡了一下午...由于里面的精度有些问题...后面输出最好用%.f不要用lf和int转换....

第一次做凸包,感觉还好...

代码:

/* *  Author:      illuz <iilluzen[at]gmail.com> *  Blog:        http://blog.csdn.net/hcbbt *  File:        poj1113.cpp *  Create Date: 2013-11-13 13:25:12 *  Descripton:  convex hull  */#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define sqr(a) ((a) * (a))#define dis(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y))const int MAXN = 1010;const double PI = acos(-1.0);struct Point {int x;int y;Point(double a = 0, double b = 0) : x(a), y(b) {}friend bool operator < (const Point &l, const Point &r) {return l.y < r.y || (l.y == r.y && l.x < r.x);}} p[MAXN], ch[MAXN];// p, point   ch, convex hulldouble mult(Point a, Point b, Point o) {return (a.x - o.x) * (b.y - o.y) >= (b.x - o.x) * (a.y - o.y);}double Graham(Point p[], int n, Point res[]) {int top = 1;sort(p, p + n);if (n == 0) return 0;res[0] = p[0];if (n == 1) return 0;res[1] = p[1];if (n == 2) return 0;res[2] = p[2];//res[2] = p[2];for (int i = 2; i < n; i++) {while (top && (mult(p[i], res[top], res[top - 1])))top--;res[++top] = p[i];}int len = top;res[++top] = p[n - 2];for (int i = n - 3; i >= 0; i--) {while (top != len && (mult(p[i], res[top], res[top - 1])))top--;res[++top] = p[i];}// clac the cdouble c = dis(res[0], res[top - 1]);for (int i = 0; i < top - 1; i++)c += dis(res[i], res[i + 1]);return c;}int n;double l;int main() {scanf("%d%lf", &n, &l);for (int i = 0; i < n; i++)scanf("%d%d", &p[i].x, &p[i].y);printf("%.f\n", (Graham(p, n, ch) + PI * l * 2));return 0;}


原创粉丝点击