题目链接:HDU - 5206
Young F found a secret record which inherited from ancient times in ancestral home by accident, which named "Four Inages Strategy". He couldn't restrain inner exciting, open the record, and read it carefully. " Place four magic stones at four points as array element in space, if four magic stones form a square, then strategy activates, destroying enemy around". Young F traveled to all corners of the country, and have collected four magic stones finally. He placed four magic stones at four points, but didn't know whether strategy could active successfully. So, could you help him?
Input
Multiple test cases, the first line contains an integer T (no more than 10000 ), indicating the number of cases. Each test case contains twelve integers x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4,|x|,|y|,|z|≤100000 ,representing coordinate of four points. Any pair of points are distinct.
Output
For each case, the output should occupies exactly one line. The output format is Case #x : ans , here x is the data number begins at 1 , if your answer is yes,ans is Yes, otherwise ans is No.
题意描述:给出四个三维坐标位置,判断这四个点能否构成一个正方形。
算法分析:比赛的时候刚开始想到正方形边边相等,然后四角90度,这样的方法搞到一半的时候想了想有点麻烦呀,后来仔细想了想,突然意识到正方形里每个顶点到其他三个顶点距离的关系:对角线距离*对角线距离=2*边*边。然后就OK啦。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 #include<vector> 8 #include<queue> 9 #define inf 0x7fffffff 10 using namespace std; 11 typedef long long LL; 12 const LL maxn=10000+10; 13 14 LL n,m; 15 struct Point 16 { 17 LL x,y,z; 18 Point (LL x=0,LL y=0,LL z=0):x(x),y(y),z(z){} 19 }a,b,c,d; 20 typedef Point Vector; 21 22 double Dot(Vector A,Vector B) {return A.x*B.x + A.y*B.y; } 23 double Length(Vector A) {return sqrt(Dot(A,A)); } 24 double angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B)); } 25 26 LL dis(Point A,Point B) 27 { 28 LL xx=(A.x-B.x)*(A.x-B.x); 29 LL yy=(A.y-B.y)*(A.y-B.y); 30 LL zz=(A.z-B.z)*(A.z-B.z); 31 return xx+yy+zz; 32 } 33 34 int main() 35 { 36 int t,ncase=1; 37 scanf("%d",&t); 38 while (t--) 39 { 40 scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d", 41 &a.x,&a.y,&a.z,&b.x,&b.y,&b.z,&c.x,&c.y,&c.z,&d.x,&d.y,&d.z); 42 //scanf("%d%d%d%d%d%d%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2,&x3,&y3,&z3,&x4,&y4,&z4); 43 int flag=0; 44 LL len,len2,len3; 45 len=dis(a,b) ;len2=dis(a,c) ;len3=dis(a,d) ; 46 if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3)) 47 flag++; 48 len=dis(b,a) ;len2=dis(b,c) ;len3=dis(b,d) ; 49 if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3)) 50 flag++; 51 len=dis(c,a) ;len2=dis(c,b) ;len3=dis(c,d) ; 52 if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3)) 53 flag++; 54 len=dis(d,a) ;len2=dis(d,b) ;len3=dis(d,c) ; 55 if ((len==len2&&len3==len+len2)||(len==len3&&len2==len+len3)||(len2==len3&&len==len2+len3)) 56 flag++; 57 if (flag==4) printf("Case #%d: Yes ",ncase++); 58 else printf("Case #%d: No ",ncase++); 59 } 60 return 0; 61 }