Four Inages Strategy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 532 Accepted Submission(s): 217
Problem Description
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.
Sample Input
2 0 0 0 0 1 0 1 0 0 1 1 0 1 1 1 2 2 2 3 3 3 4 4 4
Sample Output
Case #1: Yes Case #2: No
Source
题意:给你空间中四个点的坐标,问你这四个点的连线能否组成正方形。
分析:看到这题感觉不难,但总觉得实现有点复杂。所以这次BC直接看第二题,没想清楚解法,于是果断不做了。。吐血,以后不能这样了!!!这题记录下来主要是因为我觉得一个题目需要一个好的想法,这样实现简单并且不容易错(当然是指能做出来的题目)。由于一开始觉得实现复杂,所以把我自己给吓到了,我是想先判断四条边想等,然后对面线段平行,还有直角的判断。。各种。。其实这题只需要想法好一些就容易写出来:在p2,p3,p4 中枚举两个点作为p1 的邻点,不妨设为pi,pj ,然后判断p1pi 与p1pj 是否相等、互相垂直,然后由向量法,最后一个点坐标应该为pi+pj−p1 ,判断是否相等就好了。
code:
#include<stdio.h>
typedef long long ll;
ll Sdis(ll x,ll y,ll z,ll xx,ll yy,ll zz)
{
return (x-xx)*(x-xx)+(y-yy)*(y-yy)+(z-zz)*(z-zz);
}
int main()
{
int T;
ll x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4;
scanf("%d", &T);
for(int c=1; c<=T; c++)
{
bool flag = 0;
scanf("%lld%lld%lld %lld%lld%lld %lld%lld%lld %lld%lld%lld", &x1,&y1,&z1,&x2,&y2,&z2,&x3,&y3,&z3,&x4,&y4,&z4);
if(Sdis(x1,y1,z1,x2,y2,z2) == Sdis(x1,y1,z1,x3,y3,z3)) //两条边长度相等
{
if(Sdis(x2,y2,z2,x3,y3,z3) == 2*Sdis(x1,y1,z1,x2,y2,z2)) //判断直角,利用勾股定理
if(x4+x1 == x2+x3 && y4+y1 == y2+y3 && z4+z1 == z2+z3) flag = 1; //判断最后一个点
}
else if(Sdis(x1,y1,z1,x2,y2,z2) == Sdis(x1,y1,z1,x4,y4,z4))
{
if(Sdis(x2,y2,z2,x4,y4,z4) == 2*Sdis(x1,y1,z1,x2,y2,z2))
if(x1+x3 == x2+x4 && y1+y3 == y2+y4 && z1+z3 == z2+z4) flag = 1;
}
else if(Sdis(x1,y1,z1,x3,y3,z3) == Sdis(x1,y1,z1,x4,y4,z4))
{
if(Sdis(x3,y3,z3,x4,y4,z4) == 2*Sdis(x1,y1,z1,x3,y3,z3))
if(x1+x2 == x3+x4 && y1+y2 == y3+y4 && z1+z2 == z3+z4) flag = 1;
}
printf("Case #%d: ", c);
puts(flag ? "Yes" : "No");
}
return 0;
}
想法很重要。思路要清晰。最后不要怕。