本文主要是介绍poj 1113 凸包+简单几何计算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。
解析:
韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。
用convexhull算法把凸包求出来,然后加加减减就A了。
计算见下图:
好久没玩画图了啊好开心。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>#define LL long longusing namespace std;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-10;
const double pi = 4 * atan(1.0);struct Point
{double x, y;Point(double x = 0, double y = 0) : x(x), y(y){}
};typedef Point Vector;Vector operator + (Vector A, Vector B)
{return Vector(A.x + B.x, A.y + B.y);
}Vector operator - (Vector A, Vector B)
{return Vector(A.x - B.x, A.y - B.y);
}Vector operator * (Vector A, double p)
{return Vector(A.x * p, A.y * p);
}Vector operator / (Vector A, double p)
{return Vector(A.x / p, A.y / p);
}bool operator < (const Point& a, const Point& b)
{return a.x < b.x || (a.x == b.x && a.y < b.y);
}int dcmp(double x)
{if (fabs(x) < eps){return 0;}else{return x < 0 ? -1 : 1;}
}bool operator == (const Point& a, const Point& b)
{return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}double Dot(Vector A, Vector B)
{return A.x * B.x + A.y * B.y;
}double Length(Vector A)
{return sqrt(Dot(A, A));
}double Angle(Vector A, Vector B)
{return acos(Dot(A, B) / Length(A) / Length(B));
}double Cross(Vector A, Vector B)
{return A.x * B.y - A.y * B.x;
}double Area2(Point A, Point B, Point C)
{return Cross(B - A, C - A);
}bool cmp(Point A, Point B)
{if (A.x == B.x)return A.y < B.y;return A.x < B.x;
}int ConvexHull(Point* p, int n, Point* ch)
{sort(p, p + n, cmp);int m = 0;for (int i = 0; i < n; i++){while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)m--;ch[m++] = p[i];}int k = m;for (int i = n - 2; i >= 0; i--){while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)m--;ch[m++] = p[i];}if (n > 1)m--;return m;
}int main()
{#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALint n;double l;while (scanf("%d%lf", &n, &l) == 2){Point p[maxn];for (int i = 0; i < n; i++){scanf("%lf%lf", &p[i].x, &p[i].y);}Point ch[maxn];int index = ConvexHull(p, n, ch);
// for (int i = 0; i < index; i++)
// cout << ch[i].x << " " << ch[i].y << endl;Vector g[maxn];double angle = 0;double ans = 0;for (int i = 0; i < index - 1; i++){g[i].x = ch[i + 1].x - ch[i].x;g[i].y = ch[i + 1].y - ch[i].y;ans += Length(g[i]);}g[index - 1].x = ch[0].x - ch[index - 1].x;g[index - 1].y = ch[0].y - ch[index - 1].y;ans += Length(g[index - 1]);//cout << ans << endl;for (int i = 0; i < index - 1; i++){angle += Angle(g[i], g[i + 1]);}angle += Angle(g[index - 1], g[0]);//cout << angle << endl;ans += pi * l * angle / pi;printf("%.lf\n", ans);}return 0;
}
这篇关于poj 1113 凸包+简单几何计算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!