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. 45、文件过滤器FilenameFilter

    文件过滤器FilenameFilter JDK中提供了一个FilenameFilter的接口用来实现文件过滤功能,可以使用这个文件过滤器来实现上一节中的问题. File类中有一个带参数的list方法 ...

  2. windows 10 部署flask web

    起因 本来想这用django 写一个web 应用程序,便于管理mongodb的数据.结果django 不直接支持mongodb……又没时间研究NoSQL.于是想着随便整个api吧…… 于是先用flas ...

  3. Apple Notification Center Service--ANCS【转】

    Apple Notification Center Service 转自:http://studentdeng.github.io/blog/2014/03/22/ancs/ MAR 22ND, 20 ...

  4. Git的安装和使用(Linux)【转】

    转自:http://my.oschina.net/fhd/blog/354685 Git诞生于Linux平台并作为版本控制系统率先服务于Linux内核,因此在Linux上安装Git是非常方便的.可以通 ...

  5. Python缓存技术,装x新高度。

    一段非常简单代码 普通调用方式 def console1(a, b): print("进入函数") return (a, b) print(console1(3, 'a')) pr ...

  6. .net开源框架开源类库(整理)

    源:http://www.cnblogs.com/chinanetwind/p/3715809.html 常用库 Json.NET https://github.com/JamesNK/Newtons ...

  7. C++链接与装载

    1..obj文件的内部结构 2.映射到进程虚拟空间 3.链接的原理    C++ Code  123456789   1.未解决符号表:提供了所有在该编译单元里引用但是定义并不在本编译单元里的符号及其 ...

  8. 20165203《Java程序设计》第四周学习总结

    教材学习内容总结 第5章 子类与继承 子类的继承性 子类和父类在同一包中的继承性:子类继承父类中不是private的成员变量和方法作为自己的成员变量和方法 子类和父类不在同一包中的继承性:子类只继承父 ...

  9. Temp权限导致Win10安装程序失败提示2502/2503错误

    有Win10用户在安装程序时遇到了安装失败错误代码2502/2503的问题.如图: 其原因是Windows Installer安装程序需要当前用户具有C:\Windows\Temp目录的完全控制权限, ...

  10. STL容器读书笔记

    vector vector维护的是一个连续线性空间 vector是动态空间,随着元素的加入会自动扩容,扩充至当前size的两倍,然后将原内容拷贝,开始在原内容之后构造新元素,并释放空间 vector提 ...