【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 ...
随机推荐
- Linux基础操作命令-打包压缩
将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) 将用户信息数据库文件和用户密码数据库文件纵向合并为一个文件/2.txt(追加) 将/1.txt./2.txt两个文件打包为 ...
- NOIP 2016 迟来的满贯
17-03-22,雨 17-03-22,一个特别重要的日子 在这一天,本蒻攻克了NOIP 2016最难的一题,D1T2——天天爱跑步 实现了NOIP 2016的AK! YAYAYAYAYAYAY 自然 ...
- Interval Minimum Number
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
- 磁盘性能分析之iotop
一.安装. yum install iotop [root@localhost tmp]# iotop -o iotop命令的键盘快捷键: 1.左右箭头改变排序方式,默认是按IO排序 2.r键是反向排 ...
- docker stack 部署 filebeat
=============================================== 2018/7/21_第3次修改 ccb_warlock 更新 ...
- 【前端开发】禁止微信内置浏览器调整字体大小的方法js
微信webview内置了调整字体大小的功能,用户可以根据实际情况进行调节.但是很多移动端页面的开发都是使用rem作为单位的,字体大小改变以后,会出现页面布局错乱的情况,因此希望能够禁止微信的字体放大功 ...
- 数据库-mysql安装
MySQL 安装 所有平台的Mysql下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. Linux/UNIX上安装Mysql Linux ...
- caffe细节
1.BN层参数设置 在训练时所有BN层要设置use_global_stats: false(也可以不写,caffe默认是false) 在测试时所有BN层要设置use_global_stats: tru ...
- POJ 2195 Going Home(KM算法模板)
题目链接:http://poj.org/problem?id=2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致. man每移动一格需花费$1 ...
- SQL SERVER中查询某个表或某个索引是否存在
查询某个表是否存在: 在实际应用中可能需要删除某个表,在删除之前最好先判断一下此表是否存在,以防止返回错误信息.在SQL SERVER中可通过以下语句实现: IF OBJECT_ID(N'表名称', ...