hdu 5839 Special Tetrahedron

题意:给n个三维点,判断能构成几个满足以下条件的点:1、至少4条边相等。2、如果刚好4条边相等,那么另外两条不相邻。

思路:先将所有的点两两计算一个距离(这里可以不开方),然后将这些直线存下来,每次枚举两条相等的直线(时间复杂度会降低很多,只有在所有直线都长度都相等的情况下才会到达O(n^2),显然这种情况是很少的),然后再比较这两条直线的端点所到另一条直线的端点的距离是否和直线距离相等。当然如果他们能够组成四面体的情况下。注意去重。


大概如下图所示:

1、先找出相等的直线p1,p2(图中红色直线)

2、判断是否成四面体

3、判断黑色直线或者黄色直线是否相等,那么剩下的直线绝对不会相临。




#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
using namespace std;

struct Line{
    int len;
    int x,y;
}line[200*200];

bool cmp(const Line &a,const Line &b)
{
    return a.len < b.len;
}
int m[222][3];

struct point
{
    int x,y,z;
} px[5];
//判断是否共平面
bool isFlat(int a1,int a2,int a3,int a4)
{
    px[0].x = m[a1][0],px[0].y = m[a1][1],px[0].z = m[a1][2];
    px[1].x = m[a2][0],px[1].y = m[a2][1],px[1].z = m[a2][2];
    px[2].x = m[a3][0],px[2].y = m[a3][1],px[2].z = m[a3][2];
    px[3].x = m[a4][0],px[3].y = m[a4][1],px[3].z = m[a4][2];

    int ans;
    point s1,s2,s3;

    s1.x=px[1].x-px[0].x;
    s1.y=px[1].y-px[0].y;
    s1.z=px[1].z-px[0].z;
    s2.x=px[2].x-px[0].x;
    s2.y=px[2].y-px[0].y;
    s2.z=px[2].z-px[0].z;
    s3.x=px[3].x-px[0].x;
    s3.y=px[3].y-px[0].y;
    s3.z=px[3].z-px[0].z;
    ans=s1.x*s2.y*s3.z+s1.y*s2.z*s3.x+s1.z*s2.x*s3.y-s1.z*s2.y*s3.x-s1.x*s2.z*s3.y-s1.y*s2.x*s3.z;
    if(ans==0) return true;
    else return false;
}
//计算两点之间的长度,没开方的
int le(int i,int j)
{
    return (m[i][0] - m[j][0])*(m[i][0] - m[j][0]) +
           (m[i][1] - m[j][1])*(m[i][1] - m[j][1])+
           (m[i][2] - m[j][2])*(m[i][2] - m[j][2]);
}
struct has
{
    int  index[4];
    bool operator < (const has &x)const
    {
        if(index[0]==x.index[0])
        {
            if(index[1]==x.index[1])
            {
                if(index[2]==x.index[2])
                {
                    if(index[3]==x.index[3]){
                        return index[3]<x.index[3];
                    }
                    else return index[3]<x.index[3];
                }
                else return index[2]<x.index[2];
            }
            else return index[1]<x.index[1];
        }
        else return index[0]<x.index[0];
    }
};

set<has> vis;
int main()
{
    int t,cas = 1;
    scanf("%d",&t);
    while(t--)
    {
        vis.clear();
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d%d%d",&m[i][0],&m[i][1],&m[i][2]);
        int m=0;
        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                line[m].len=le(i,j);
                line[m].x = i;
                line[m++].y = j;
            }

        }
        long long ans = 0;
        sort(line,line+m,cmp);
        for(int i =0; i<m; i++)
        {
            Line p1 = line[i];
            for(int j = i+1; p1.len == line[j].len; j++)
            {
                Line p2 = line[j];
                if(isFlat(p1.x,p1.y,p2.x,p2.y)) continue;

                if(p1.x !=p2.y && p2.x != p1.y)
                {
                    int len1 = le(p1.x,p2.x);
                    int len2 = le(p1.x,p2.y);
                    if((len1 == le(p1.y,p2.y) && len1 == p1.len )||( len2== le(p1.y,p2.x) && len2 == p2.len))
                    {
                        has aa;
                        aa.index[0] = p1.x;
                        aa.index[1] = p1.y;
                        aa.index[2] = p2.x;
                        aa.index[3] = p2.y;
                        sort(aa.index,aa.index+4);
                        if(vis.count(aa) != 1){
                            vis.insert(aa);
                            ans++;
                        }

                    }

                }
            }

        }
        printf("Case #%d: %I64d\n",cas++,ans);

    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值