POJ 1696 卷包裹算法
2016-03-16 16:51
323 查看
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <map> #include <cmath> using namespace std; const int maxn = 100 + 10; struct Point { double x, y; Point(double x = 0, double y = 0): x(x), y(y) {} }; typedef Point Vector; typedef vector<Point> Polygon; Vector operator +(Vector A, Vector B)// { return Vector(A.x + B.x, A.y + B.y); } Vector operator -(Point A, Point 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.y < b.y || (a.y == b.y && a.x < b.x); } const double eps = 1e-10; 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 Cross(Vector A, Vector B)// { return A.x * B.y - A.y * B.x; } Point read_point() { double X, Y; scanf("%lf%lf", &X, &Y); return Point(X, Y); } int T, t, n; Point P[maxn]; void extend_ConvexHull(Point *p, int n) { map<Point, int>mp; for (int i = 0; i < n; i++) mp[p[i]] = i; bool vis[maxn]; memset(vis, 0, sizeof(vis)); sort(p, p + n); vector<int>ans; ans.push_back(mp[p[0]]); vis[0] = 1; int next = 0, cur = 0; for (int m = 1; m < n; m++) { for (int i = 0, first = 1; i < n; i++) { if (vis[i]) continue; if (first) next = i, first = 0; if (dcmp(Cross(p[next] - p[cur], p[i] - p[cur]) < 0) || (dcmp(Cross(p[next] - p[cur], p[i] - p[cur]) == 0) && dcmp(Length(p[i] - p[cur]) - Length(p[next] - p[cur])) < 0)) next = i; } vis[next] = 1; cur = next; ans.push_back(mp[p[next]]); } printf("%d ", n); for (int i = 0; i < n; i++) printf("%d%c", ans[i] + 1, (i == n - 1 ? '\n' : ' ')); } int main(int argc, char const *argv[]) { scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &t); P[i] = read_point(); } extend_ConvexHull(P, n); } return 0; }
要求的是原来的标号,这个相当恶心,搞了一个map来存的。
很显然,这不是要求凸包,但只要把卷包裹算法修改一下就可以了。
卷包裹算法:http://www.cnblogs.com/Booble/archive/2011/02/28/1967179.html
凸包的五大算法:http://blog.csdn.net/bone_ace/article/details/46239187
相关文章推荐
- MVC
- 工作备忘-添加Xcode插件不小心跳过了 还怎么添加?
- linux系统安装mysql
- centos7服务管理组件systemctl的服务存贮目录和常用列表命令
- Android 四种线程池
- 常见的布局或控件问题
- ArrayList的使用方法【转载】
- Jupyter的安装和配置
- android 提高性能点
- hdu 1166 敌兵布阵(线段树,单点更新)
- Android 将ARGB图片转换为灰度图
- JAVA IO(2)
- Android系统下的动态Dex加载
- 怎样能在大小不同的屏幕上显示同样的网页(自适应网页设计)?
- Tachyon:Spark生态系统中的分布式内存文件系统
- Javabean
- super_vars.php ref_var.php
- 获取QQ企业邮箱通讯录PY脚本
- 1302
- 反转ListBox的ListBoxItem(控件级别,不是数据的反转)