HDU-5839 Special Tetrahedron(纯暴力)

来源:互联网 发布:自动加粉软件 编辑:程序博客网 时间:2024/06/05 15:49


Special Tetrahedron

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


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
240 0 00 1 11 0 11 1 090 0 00 0 21 1 1-1 -1 11 -1 1-1 1 11 1 01 0 10 1 1
 

Sample Output
Case #1: 1Case #2: 6
 

题意:在空间中的点里面找到有多少点可以组成满足下列条件的四面体:
1.至少有四条边相同.
2.在确保4条边相等的情况下,另外的两条边不相邻。
QAQ,昨天4道题止步于网络赛,奈何这个第五道三维几何没做过,被吓住了 TAT ..根本没有1003难嘛。。
题解:枚举对角线,找到所有和对角线两端点相等的点,然后去枚举所有的和对角线距离相等的点(还要判断四点不共面)。因为有两条对角线,所以答案会被算两次。然后是正四面体,我们每条线都被多算了1次,总共算了6次,我们只要其中的一次。所以最终答案为 (ans1/6+ans2/2)交代码请用G++。。

n^4暴力,但实际上复杂度达不到n^4【和出题人是否懒惰有关,233

暴力枚举两个点,然后再枚举离这两点距离相同的点。

再枚举四个点,找到这个四边形四边相同,但是不共面的四个点。

再判断这个是不是正四边形,如果是正四边形的话,你会重复计算6次

否则重复计算两次。

代码如下:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<limits.h>#include<queue>#include<math.h>#include<stack>#include<vector>#include<algorithm>using namespace std;#define maxn  205struct  node{int x;int y;int z;}a[maxn];long long dis(node a,node b);int sq(int a);bool check(node a,node b,node c,node d);bool check(node a,node b,node c,node d){    node s1,s2,s3;    s1.x=b.x-a.x;s1.y=b.y-a.y;s1.z=b.z-a.z;    s2.x=c.x-a.x;s2.y=c.y-a.y;s2.z=c.z-a.z;    s3.x=d.x-a.x;s3.y=d.y-a.y;s3.z=d.z-a.z;    long long 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 1;//如果三个方向向量组成的行列式为0,则四点共面     return 0;}int sq(int a){return a*a;}long long dis(node a,node b){return sq(a.x-b.x)+sq(a.y-b.y)+sq(a.z-b.z);}int s[maxn];int  main(){int T,n,m,i,j,k,h,ans,cases=0;long long ans1,ans2;scanf("%d",&T);while(T--){memset(s,0,sizeof(s));memset(a,0,sizeof(a));ans1=0;ans2=0;scanf("%d",&n);for(i=1;i<=n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);for(i=1;i<=n;i++){for(j=1+i;j<=n;j++){int cnt=0;for(k=1;k<=n;k++){if(k==i || k==j)  continue;if(dis(a[i],a[k])==dis(a[j],a[k]))  s[cnt++]=k;}if(cnt<=1)   continue;else for(h=0;h<cnt;h++) { for(int h1=h+1;h1<cnt;h1++) { int id1=s[h],id2=s[h1]; if(dis(a[id1],a[i])!=dis(a[id2],a[i]))//相邻的两条边要相等     continue; if(check(a[i],a[j],a[id1],a[id2]))//判断四点是否共面    continue; if(dis(a[id1],a[id2])==dis(a[i],a[j]) && dis(a[id1],a[i])==dis(a[i],a[j]))   ans1++; else   ans2++;  } }}}printf("Case #%d: %d\n",++cases,ans1/6+ans2/2);}}


0 0
原创粉丝点击