http://acm.hdu.edu.cn/showproblem.php?pid=3662

求给定空间中的点的三维凸包上有多少个面。

用增量法,不断加入点,把新加的点能看到的面都删掉,不能看到的面与能看到的面的棱与新点相连构成一个新的三角形面。

这样的面全都是三角形,注意最后统计答案时要把重合的面算成一个。

时间复杂度\(O(n^2)\)。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 303;
const double eps = 1e-6; struct Point {
double x, y, z;
Point(double _x = 0, double _y = 0, double _z = 0) : x(_x), y(_y), z(_z) {}
Point operator + (const Point &A) const {
return Point(x + A.x, y + A.y, z + A.z);
}
Point operator - (const Point &A) const {
return Point(x - A.x, y - A.y, z - A.z);
}
double operator * (const Point &A) const {
return x * A.x + y * A.y + z * A.z;
}
Point operator ^ (const Point &A) const {
return Point(y * A.z - z * A.y, z * A.x - x * A.z, x * A.y - y * A.x);
}
double sqrlen() {
return x * x + y * y + z * z;
}
} P[N]; struct Face {
int a, b, c; bool ex;
Face(int _a = 0, int _b = 0, int _c = 0, bool _ex = false) : a(_a), b(_b), c(_c), ex(_ex) {}
} F[N * N]; int n, ftot, LeftFace[N][N]; void insFace(int a, int b, int c, int n1, int n2, int n3) {
F[++ftot] = (Face) {a, b, c, true};
LeftFace[a][b] = LeftFace[b][c] = LeftFace[c][a] = ftot;
LeftFace[b][a] = n1;
LeftFace[c][b] = n2;
LeftFace[a][c] = n3;
} bool visible(int f, int p) {
Point a = P[F[f].b] - P[F[f].a], b = P[F[f].c] - P[F[f].a];
return (P[p] - P[F[f].a]) * (a ^ b) > eps;
} int st, to[N], ps[N], pt[N], ptot = 0, pf[N]; void dfs(int x, int s, int t, int p) {
if (F[x].ex == false) return; if (visible(x, p))
F[x].ex = false;
else {
to[st = s] = t;
return;
} dfs(LeftFace[F[x].b][F[x].a], F[x].a, F[x].b, p);
dfs(LeftFace[F[x].c][F[x].b], F[x].b, F[x].c, p);
dfs(LeftFace[F[x].a][F[x].c], F[x].c, F[x].a, p);
} Point ff;
void dfs2(int x) {
if (F[x].ex == false) return; Point now = (P[F[x].b] - P[F[x].a]) ^ (P[F[x].c] - P[F[x].a]);
if (fabs(now * ff - sqrt(now.sqrlen() * ff.sqrlen())) < 1e-6)
F[x].ex = false;
else
return; dfs2(LeftFace[F[x].b][F[x].a]);
dfs2(LeftFace[F[x].c][F[x].b]);
dfs2(LeftFace[F[x].a][F[x].c]);
} int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; ++i) scanf("%lf%lf%lf", &P[i].x, &P[i].y, &P[i].z);
ftot = 0;
Point a, b, c, d, e;
a = P[2] - P[1];
int tmp, id1, id2;
for (tmp = 3; tmp <= n; ++tmp) {
b = P[tmp] - P[1];
d = a ^ b;
if (d.sqrlen() < eps) continue;
id1 = tmp; break;
}
for (++tmp; tmp <= n; ++tmp) {
c = P[tmp] - P[1];
if (fabs(d * c) < eps) continue;
id2 = tmp; break;
} if (d * c < 0) swap(id1, id2);
insFace(1, 2, id2, 3, 4, 2);
insFace(1, id2, id1, 1, 4, 3);
insFace(1, id1, 2, 2, 4, 1);
insFace(2, id1, id2, 3, 2, 1); for (int i = 3; i <= n; ++i) {
if (i == id1 || i == id2) continue;
for (int j = 1; j <= ftot; ++j)
if (F[j].ex && visible(j, i)) {
dfs(j, 0, 0, i);
ptot = 0;
int tmps = st, tmpt = to[st], ppff = ftot;
do {
++ptot;
ps[ptot] = tmps; pt[ptot] = tmpt;
pf[ptot] = ++ppff;
tmps = tmpt; tmpt = to[tmpt];
} while (tmps != st); for (int k = 1, pre, suc; k <= ptot; ++k) {
pre = k - 1; suc = k + 1;
if (pre == 0) pre = ptot;
if (suc > ptot) suc = 1;
pre = pf[pre]; suc = pf[suc];
insFace(pt[k], i, ps[k], suc, pre, LeftFace[pt[k]][ps[k]]);
} break;
}
} int ans = 0;
for (int i = 1; i <= ftot; ++i)
if (F[i].ex) {
++ans;
ff = (P[F[i].b] - P[F[i].a]) ^ (P[F[i].c] - P[F[i].a]);
dfs2(i);
}
printf("%d\n", ans);
} return 0;
}

【HDU 3662】3D Convex Hull的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 2108】Shape of HDU

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=2108 [题意] [题解] 逆时针; 可以想象一下; 如果是凸多边形的话; 逆时针的相邻的两条边; ...

  8. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  9. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

随机推荐

  1. Unity 3(一):简介与示例

    本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): Ioc/DI简介: Unity简单示例 一.Ioc/DI简介 IoC 即 Inversion of C ...

  2. 2016.6.21——Climbing Stairs

    Climbing Stairs 本题收获: 1.斐波那契函数f(n) = f(n-1) + f(n -2) 题目: You are climbing a stair case. It takes n ...

  3. Dream------Hadoop--FSDataInputStream和FSDataOutputStream

    一.FSDataInputStream    FileSystem中的open()方法实际上返回的是一个FSDataInputStream,而不是标准的java.io类.这个类是java.io.Dat ...

  4. 可能是最漂亮的Spring事务管理详解

    Java面试通关手册(Java学习指南):https://github.com/Snailclimb/Java_Guide 微信阅读地址链接:可能是最漂亮的Spring事务管理详解 事务概念回顾 什么 ...

  5. 网页中嵌入swf文件的几种方法

    1. object + embed       传统的方法 优点:浏览器兼容性好,是 Macromedia 一直以来的官方方法缺点:a.embed 标签是不符合 W3C 的规范的,无法通过验证.当然, ...

  6. MySQL5.7之多源复制&Nginx中间件(上)【转】

    有生之年系列----MySQL5.7之多源复制&Nginx中间件(上)-wangwenan6-ITPUB博客http://blog.itpub.net/29510932/viewspace-1 ...

  7. Python Challenge 第 4 关攻略:linkedlist

    代码 import requests url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing={}" ...

  8. mysql高可用架构 -> MHA配置binlog-server-06

    前期准备 1.准备一台新的mysql实例(db03),GTID必须开启. 2.将来binlog接收目录,不能和主库binlog目录一样 停止mha masterha_stop --conf=/etc/ ...

  9. idea中JDK失效

    [问题] 在没有改变任何东西的情况下,突然间IDEA里面所有的代码都标红,无法找到JDK [解决方法] [File]->[Invalidate Caches],然后就好了

  10. 03 Go 1.3 Release Notes

    Go 1.3 Release Notes Introduction to Go 1.3 Changes to the supported operating systems and architect ...