HDU 5839 Special Tetrahedron (计算几何)
Special Tetrahedron
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5839
Description
Given n points which are in three-dimensional space(without repetition).
Please find out how many distinct Special Tetrahedron among them. A tetrahedron is called Special Tetrahedron if it has two following characters.
1. At least four edges have the same length.
2. If it has exactly four edges of the same length, the other two edges are not adjacent.
Input
Intput contains multiple test cases.
The first line is an integer T,1≤T≤20, the number of test cases.
Each case begins with an integer n(n≤200), indicating the number of the points.
The next n lines contains three integers xi,yi,zi, (−2000≤xi,yi,zi≤2000), representing the coordinates of the ith point.
Output
For each test case,output a line which contains"Case #x: y",x represents the xth test(starting from one),y is the number of Special Tetrahedron.
Sample Input
2
4
0 0 0
0 1 1
1 0 1
1 1 0
9
0 0 0
0 0 2
1 1 1
-1 -1 1
1 -1 1
-1 1 1
1 1 0
1 0 1
0 1 1
Sample Output
Case #1: 1
Case #2: 6
Source
2016中国大学生程序设计竞赛 - 网络选拔赛
##题意:
在三维空间中给出N个点,找有多少个满足条件的四面体.
1. 四面体至少有四条边相等
2. 若恰好四条边相等,那么剩下的两边不相邻.
##题解:
四面体中每条边仅有一条边与它不相邻, 可以转化一下题目的条件:
寻找四边相等的空间四边形(不能四点共面).
首先枚举该空间四边形中的一条对角线,再计算剩余的点跟这条对角线两端点的距离.
若某点与这对角线两端点的距离相等, 则添加到一个集合中.
枚举集合中的任意两点,与对角线两端点组成一个四边形. 判断该四边形是否符合条件:
首先集合中两点到对角线端点的距离要相等. 其次,这四点要不共面.
在上述判断过程中会出现重复计数:
由于每个四边形有两条对角线,所以在枚举对角形计数时,每个四边形都被计数了两次.
特殊的:如果一个四面体是正四面体,那么这四个点被计数了6次. (每条边都计数一次)
所以在处理过程中要记录出现了几次正四面体.
每当枚举到一个合法的空间四面体时,求一下剩下两条边是否跟其它边相等. 如果相等则计数.
令rep为上述计数次数, rep/6 为正四面体的个数(每个正四面体会计数6次).
根据上述分析, ans/2 - 2*rep/6 即为最后结果.
关于时间复杂度:
上述过程看起来是 O(N^4).
而实际上,每次枚举对角线时,符合条件的点一定在这条对角线的中垂面上.
所以不可能每次枚举对角线时,都有很多点在中垂面上. 所以均摊复杂度并不高.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define double long long
#define eps 1e-8
#define maxn 1010
#define mod 1000000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
struct Point{
double x,y,z;
Point(){}
Point(double tx,double ty) {x=tx;y=ty;}
}p[220];
double Distance(Point p1,Point p2)
{
return ((p1.x-p2.x)(p1.x-p2.x)+(p1.y-p2.y)(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z));
}
Point xmul(Point u, Point v) {
Point ret;
ret.x = u.yv.z - v.yu.z;
ret.y = u.zv.x - u.xv.z;
ret.z = u.xv.y - u.yv.x;
return ret;
}
LL dmul(Point u, Point v) {
return u.xv.x + u.yv.y + u.z*v.z;
}
Point subt(Point u, Point v) {
Point ret;
ret.x = u.x - v.x;
ret.y = u.y - v.y;
ret.z = u.z - v.z;
return ret;
}
/求平面的垂向量/
Point pvec(Point s1, Point s2, Point s3) {
return xmul(subt(s1,s2),subt(s2,s3));
}
/判断四点共面/
bool is_ok(Point a, Point b, Point c, Point d) {
return (dmul(pvec(a,b,c),subt(d,a))) == 0LL;
}
struct node {
int id;
LL dis;
node(){}
node(int tx,LL ty) {id=tx;dis=ty;}
};
vector q;
int main(int argc, char const *argv[])
{
//IN;
int t, ca=1; cin >> t;
while(t--)
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%lld %lld %lld", &p[i].x, &p[i].y, &p[i].z);
int ans = 0;
int rep = 0;
for(int i=1; i<=n; i++) {
for(int j=i+1; j<=n; j++) { /*枚举对角线*/
q.clear();
for(int k=1; k<=n; k++) {
if(Distance(p[k],p[i]) == Distance(p[k],p[j])) {
q.push_back(node(k, Distance(p[k],p[j])));
}
}
int sz = q.size();
for(int ii=0; ii<sz; ii++) {
for(int jj=ii+1; jj<sz; jj++) {
if(q[ii].dis != q[jj].dis) continue;
if(is_ok(p[i],p[j],p[q[ii].id],p[q[jj].id])) continue; /*四点共面*/
ans++;
/*是否是正四面体*/
if(Distance(p[i],p[j])==q[ii].dis && q[ii].dis==Distance(p[q[ii].id],p[q[jj].id]))
rep++;
}
}
}
}
ans /= 2;
ans -= 2*rep/6;
printf("Case #%d: %d\n", ca++, ans);
}
return 0;
}
HDU 5839 Special Tetrahedron (计算几何)的更多相关文章
- HDU 5839 Special Tetrahedron 计算几何
Special Tetrahedron 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5839 Description Given n points ...
- HDU 5839 Special Tetrahedron
HDU 5839 Special Tetrahedron 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5839 Description Given n ...
- HDU 5839 Special Tetrahedron (2016CCPC网络赛08) (暴力+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5839 在一个三维坐标,给你n个点,问你有多少个四面体(4个点,6条边) 且满足至少四边相等 其余两边不 ...
- HDU 5130 Signal Interference(计算几何 + 模板)
HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...
- (四面体)CCPC网络赛 HDU5839 Special Tetrahedron
CCPC网络赛 HDU5839 Special Tetrahedron 题意:n个点,选四个出来组成四面体,要符合四面体至少四条边相等,若四条边相等则剩下两条边不相邻,求个数 思路:枚举四面体上一条线 ...
- hdu 5839(三维几何)
Special Tetrahedron Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 【HDU 5839】Special Tetrahedron(计算几何)
空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...
- HDU 5979 Convex【计算几何】 (2016ACM/ICPC亚洲区大连站)
Convex Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- openfire中mysql的前期设置
使用openfire的时候如果需要使用自己的mysql数据库,需要提前进行设置,下面将记录下,基本的设置过程. 一.前期准备工作: 1.先下载两个工具一个是mysql数据库还有一个是SQLyog(可以 ...
- 设置MySQL主从同步
1. 配置主服务器 1.1 编辑my.cnf文件,配置主服务器ID. [mysqld] log-bin=mysql-bin server-id=1relay-log = relay-bin relay ...
- 8天学通MongoDB——第五天 主从复制
从这一篇开始我们主要讨论mongodb的部署技术. 我们知道sql server能够做到读写分离,双机热备份和集群部署,当然mongodb也能做到,实际应用中我们不希望数据库采用单点部署, 如果碰到数 ...
- JAVA将Excel中的报表导出为图片格式(二)实现思路
接上文,一封类似于下方设计的Excel报表,如何将它指定的区域导出为样式一模一样的JPG图片呢? 要实现这个功能没有现成的解决方案,谷歌度娘了好久也没有,最终自己想了几条思路: 思路1:将报表中的背景 ...
- sql server 数据库 ' ' 附近有语法错误
昨天做项目时候,遇到标题的问题,代码跟踪把sql 语句 复制出来在数据库执行不了, 然后重新写个一模一样的,然后在 赋值到代码中,还是同样的错误, 就是不知道哪里出现了错误,最后 把 sql 语句写成 ...
- Qt之自定义界面(右下角冒泡)
简述 网页右下角上经常会出现一些提示性的信息,桌面软件中也比较常见,类似360新闻.QQ消息提示一样! 这种功能用动画实现起来很简单,这节我们暂时使用定时器来实现,后面章节会对动画框架进行详细讲解. ...
- javascript插件编写小结
写JS插件,最好是先通过HTML方式将展示结果显示出来,然后再封装成JS插件,将其画出来.JS模板如下: (function($){ $.fn.fnName = function(options){ ...
- 前端SPA框架一些看法
说起前端框架,我个人主张有框架不如无框架,这个观点要先从框架和库的区别说起. 我所理解的库,解决的是代码或是模块级别的复用或者对复杂度的封装问题;而框架,更多的是对模式级别的复用和对程序组织的规范,这 ...
- hdu 4614 Vases and Flowers(线段树:成段更新)
线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...
- datatables 参数详解(转)
//@translator codepiano //@blog codepiano //@email codepiano.li@gmail.com //尝试着翻译了一下,难免有错误的地方,欢迎发邮件告 ...