【HDU 3662】3D Convex Hull
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的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 2108】Shape of HDU
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=2108 [题意] [题解] 逆时针; 可以想象一下; 如果是凸多边形的话; 逆时针的相邻的两条边; ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
随机推荐
- Shell基础-通配符
* - 通配符,代表任意字符 ? - 通配符,代表一个字符 # - 注释 | - 分隔两个管线命令的界定 ; - 连续性命令的界定 ~ - 用户的根目录 $ - 变量前需要加的变量值 ! - 逻辑运算 ...
- 洛谷 P4175: bzoj 1146: [CTSC2008]网络管理
令人抓狂的整体二分题.根本原因还是我太菜了. 在学校写了一个下午写得头晕,回家里重写了一遍,一个小时就写完了--不过还是太慢. 题目传送门:洛谷P4175. 题意简述: 一棵 \(n\) 个结点的树, ...
- 个性化你的Git Log的输出格式
git已经变成了很多程序员日常工具之一. git log是查看git历史的好工具,不过默认的格式并不是特别的直观. 很多时候想要更简便的输出更多或者更少的信息,这里列出几个git log的format ...
- qt使用动态库(DLL)
本文主要讲解在QT开发环境中如何使用VC生成的DLL及QT自身生成的DLL.至于其它情况本文不作讨论. 连接库分为2种 (1)动态连接库,通常有.h .lib .dll三个文件,功能实现在dll中 ( ...
- C/C++杂记:NULL与0的区别、nullptr的来历
某些时候,我们需要将指针赋值为空指针,以防止野指针. 有人喜欢使用NULL作为空指针常量使用,例如:int* p = NULL;. 也有人直接使用0值作为空指针常量,例如:int* p = 0;. ...
- 数据库-python操作mysql(pymsql)
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同 一:安装pymysql pip3 install pymysql 二:使用pytmysql # -*- codin ...
- java基础33 Set集合下的HashSet集合和TreeSet集合
单例集合体系: ---------| collection 单例集合的根接口--------------| List 如果实现了list接口的集合类,具备的特点:有序,可重复 注:集合 ...
- 洛谷P3621风铃
传送门啦 分析: 这个题看起来像是个树形dp,嗯,就是看起来像. 所以我们就按树形dp的思路去分析就好了,这个题是一个树形dp的变形题. 和以前建树是一样的,我们用邻接表来进行储存.利用邻接表的特性, ...
- nio复习总结
观察者: 多个对象依赖一个对象的状态, 当这个对象状态发生改变时,依次通知多个对象. 消息的分发和处理 事件驱动 / IO多路复用 借助select epoll等 reactor: io事件触发时, ...
- maven pom.xml配置
<repositories> <repository> <id>central</id> <name>Maven Repository Sw ...