hdu 5839(三维几何)

本文介绍了一道关于三维空间中寻找特殊四面体的算法题目——SpecialTetrahedron。特殊四面体是指至少有四条边长度相等且在四条边相等的情况下,其余两条边不相邻的四面体。文章提供了详细的解题思路及代码实现。

Special Tetrahedron

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 328    Accepted Submission(s): 130


Problem 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,1T20, the number of test cases.

Each case begins with an integer n(n200), indicating the number of the points.

The next n lines contains three integers xi,yi,zi, (2000xi,yi,zi2000), 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
 
题意:在空间中的点里面找到有多少点可以组成满足下列条件的四面体:
1.至少有四条边相同.
2.在确保4条边相等的情况下,另外的两条边不相邻。
QAQ,昨天4道题止步于网络赛,奈何这个第五道三维几何没做过,被吓住了 TAT ..根本没有1003难嘛。。
题解:枚举对角线,找到所有和对角线两端点相等的点,然后去枚举所有的和对角线距离相等的点(还要判断四点不共面)。因为有两条对角线,所以答案会被算两次。然后是正四面体,我们每条线都被多算了1次,总共算了6次,我们只要其中的一次。所以最终答案为 (ans-same)/2+same/6 = ans/2- same/3...交代码请用G++。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#define sqr(x) ((x)*(x))
using namespace std;
const double eps = 1e-8;
struct Point
{
    double x,y,z;
    Point(double x, double y, double z) : x(x), y(y), z(z) {}
    Point() {}
    Point operator - (const Point & p) const
    {
        return Point(x-p.x, y-p.y, z-p.z);
    }
} p[250];
struct Node
{
    int idx;
    double dis;
}node[250];
int sig(double d)
{
    return (d>eps) - (d<-eps);
}
//叉乘
Point cross(const Point & a, const Point & b)
{
    return Point(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
}
Point cross(const Point & o, const Point & a, const Point & b)
{
    return cross(a-o,b-o);
}
//点乘
double dot(const Point & a, const Point & b)
{
    return a.x*b.x + a.y*b.y + a.z*b.z;
}
//判断四点共面
bool sameFace(const Point & a, const Point & b, const Point & c, const Point & d)
{
    return sig(dot(b-a, cross(a, c, d))) == 0;
}
//两点距离
double dis(const Point & a, const Point & b)
{
    return sqrt(sqr(a.x-b.x) + sqr(a.y-b.y) + sqr(a.z-b.z));
}
int main()
{
    int tcase,n,t=1;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z);
        }
        int ans = 0,ans1=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++) ///枚举对角线
            {
                int cnt = 0;
                for(int k=1; k<=n; k++)
                {
                    if(sig(dis(p[i],p[k])-dis(p[j],p[k]))==0)
                    {
                        node[++cnt].idx = k;
                        node[cnt].dis = dis(p[i],p[k]);
                    }
                }
                for(int k=1; k<=cnt; k++)
                {
                    for(int l=k+1; l<=cnt; l++)
                    {
                        if(sig(node[k].dis-node[l].dis)!=0) continue;
                        if(sameFace(p[i],p[j],p[node[k].idx],p[node[l].idx])) continue;
                        ans++;
                        if(sig(dis(p[node[k].idx],p[node[l].idx])-node[k].dis)==0&&sig(dis(p[i],p[j])-node[k].dis)==0)
                        {
                            ans1++;
                        }
                    }
                }
            }
        }
        printf("Case #%d: %d\n",t++,ans/2-ans1/3);
    }
}

 

 

转载于:https://www.cnblogs.com/liyinggang/p/5772161.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值