Four Inages Strategy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 620 Accepted Submission(s): 246
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
刷这道题目竟然花了我两天的时间,我简直都要疯了!!!!!
最后得出的错因竟然是数字类型开的太小,我本来以为比较乘积的话,就可以避免这样的错误,结果没想到还是错了。
我大体的思路是,从一点出发,作到另外三个点的三个向量,然后对三个向量进行关于长度的排序,接着比较:最短的两条向量模是否相等,最短的两条向量的和是否为长的那条向量,最短的两条向量是否垂直,这三点如果能满足,一般就是正方形了。
以下是AC代码,速度还蛮快的:
#include<stdio.h>
#include<algorithm>
#define dis(a,b) (a.x*b.x+a.y*b.y+a.z*b.z)
#include<ctype.h>
using namespace std;
typedef long long ll;
struct node
{
ll x,y,z;
}point[3];
int compare(node a,node b)
{
return dis(a,a)<dis(b,b);
}
int check(node a,node b,node c)
{
return a.x+b.x==c.x&&a.y+b.y==c.y&&a.z+b.z==c.z;
}
int main()
{
int t;
node temp;
ll x1,y1,z1;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%lld%lld%lld",&x1,&y1,&z1);
for(int j=0;j<3;j++)
{
scanf("%lld%lld%lld",&temp.x,&temp.y,&temp.z);
point[j].x=temp.x-x1;
point[j].y=temp.y-y1;
point[j].z=temp.z-z1;
}
sort(point,point+3,compare);
if(dis(point[0],point[0])==dis(point[1],point[1])&&!dis(point[0],point[1])&&check(point[0],point[1],point[2]))
printf("Case #%d: Yes\n",i);
else
printf("Case #%d: No\n",i);
}
return 0;
}