HDU 5839 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.
  1. If it has exactly four edges of the same length, the other two edges are not adjacent.

Input

t 组数据,n(n <= 200)个点,每个点的三维坐标。

Output

求出多少个四面体满足条件。

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

题意:

给你最多200个点让你找出其中不同的四面体,要求这个四面体至少四条边相同,如果只有四条边相同,剩下的两条边不共顶点。

题解:

做题的时候想到了这么做但是一分析n^4的复杂度就放弃了,结果结束后看别人的题解发现这个复杂度加上剪枝可过,怼了一发。

首先是枚举三个点,如果三点不组成一个平面,或者三点组成的三角形三边都不相同,那么继续枚举下一组三点。如果枚举的三点满足条件,再枚举剩下的可以作为第四个的点,判断是否满足条件。这样的四点组数就出来了。

因为至少四条边相同。现在我们先以任一点为顶点,如果它到其他三点距离相同,那么如果剩下的三个点是等腰三角形则符合条件。如果到其他三点距离有两个相同,那么我枚举三组相同的边,则不同的边以及对边就出来了,可以容易判断。至于定点到底面三边不相同直接不符合条件。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct Point {
LL x,y,z;
Point operator - (Point &R)const{
Point ret;
ret.x = x - R.x;
ret.y = y - R.y;
ret.z = z - R.z;
return ret;
}
}p[300];
int n;
inline LL line2(Point &a,Point &b)
{
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z);
}
inline LL gx(Point &a,Point &b,Point &c)
{
Point l1,l2;
l1 = a-b;
l2 = a-c;
if ((l1.x*l2.y == l1.y*l2.x) && (l1.x*l2.z == l1.z*l2.x) && (l1.y*l2.z == l1.z*l2.y)) return true;
return false;
}
inline LL fourm(Point &d,Point &a,Point &b,Point &c)
{
LL s[4][4];
s[1][1] = d.x-a.x;s[1][2] = d.y-a.y;s[1][3] = d.z-a.z;
s[2][1] = d.x-b.x;s[2][2] = d.y-b.y;s[2][3] = d.z-b.z;
s[3][1] = d.x-c.x;s[3][2] = d.y-c.y;s[3][3] = d.z-c.z;
LL ans1,ans2;
ans1 = s[1][1]*s[2][2]*s[3][3] + s[1][2]*s[2][3]*s[3][1] + s[1][3]*s[2][1]*s[3][2];
ans2 = s[1][3]*s[2][2]*s[3][1] + s[1][1]*s[2][3]*s[3][2] + s[1][2]*s[2][1]*s[3][3];
if (ans1 == ans2) return false;
return true;
}
inline bool fin(Point &d,Point &a,Point &b,Point &c)
{
LL l1,l2,l3,l4,l5,l6;
l1 = line2(a,b);
l2 = line2(b,c);
l3 = line2(a,c);
l4 = line2(a,d);
l5 = line2(b,d);
l6 = line2(d,c);
if (l4 == l5 && l5 == l6){
if ( ((l1 == l3) && (l1 == l4)) || ((l1 == l2) && (l1 == l4)) || ((l2 == l3)&&(l2 == l4)) ) return true;
}else if (l4 == l5){
if (l2 == l3 && l2 == l4) return true;
}else if (l5 == l6){
if (l1 == l3 && l1 == l5) return true;
}else if (l4 == l6){
if (l1 == l2 && l1 == l4) return true;
}
return false;
}
inline int solve()
{
int ans = 0;
LL ll1,ll2,ll3;
for (int d1 = 1; d1 <= n; d1++){
for (int d2 = d1+1; d2 <= n; d2++){
ll3 = line2(p[d1],p[d2]);
for (int d3 = d2+1; d3 <= n; d3++){
ll1 = line2(p[d2],p[d3]);
ll2 = line2(p[d1],p[d3]);
if (ll1 != ll2 && ll1 != ll3 && ll2 != ll3)
continue;
if (gx(p[d1],p[d2],p[d3]))
continue;
for (int d4 = d3+1; d4 <= n; d4++){
if (fourm(p[d4],p[d1],p[d2],p[d3])){
bool te = false;
if (fin(p[d4],p[d1],p[d2],p[d3])){
ans++;
}
}
}
}
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
for (int _t = 1; _t <= t; _t++){
scanf("%d",&n);
for (int i = 1; i <= n; i++) scanf("%lld %lld %lld",&p[i].x,&p[i].y,&p[i].z);
printf("Case #%d: %d\n",_t,solve());
}
return 0;
}