[BC]Four Inages Strategy(三维空间判断正方形)

题目连接 :http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=577&pid=1001

题目大意:在三维空间中,给你四个点,判断是否可以组成一个正方形:

解题思路:首先判断四条边是否相等,判断方法取三个边如果两边相等且平方和相加等于第三边平方和即可,在判断是否有一个角为直角,判断方法取三点叉乘为0就为直角。

AC代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct point
{
    int x;
    int y;
    int z;
}p[5];
int dis(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);
}
int cheng(point a,point b,point c)
{
    return (a.x-b.x)*(c.x-a.x)+(a.y-b.y)*(c.y-a.y)+(a.z-b.z)*(c.z-a.z);
}
int fun(int a,int b,int c)
{
    if((a==b&&a+b==c)||(a==c&&a+c==b)||(c==b&&c+b==a)) return 1;
    else return 0;
}
int judge()
{
    int a,b,c,a2,b2,c2,a3,b3,c3,t;
    a=dis(p[0],p[1]),b=dis(p[0],p[2]),c=dis(p[0],p[3]);
    a2=dis(p[1],p[0]),b2=dis(p[1],p[2]),c2=dis(p[1],p[3]);
    a3=dis(p[2],p[0]),b3=dis(p[2],p[1]),c3=dis(p[2],p[3]);
    if(fun(a,b,c)&&fun(a2,b2,c2)&&fun(a3,b3,c3))
    {
        int ab,bc,ac;
        ab=cheng(p[0],p[1],p[2]);
        bc=cheng(p[1],p[2],p[0]);
        ac=cheng(p[2],p[0],p[1]);
        if(ab==0||bc==0||ac==0) return 1;
    }
    return 0;
}
int main()
{
    int i,k,tcase;
    scanf("%d",&tcase);
    for(k=1;k<=tcase;k++)
    {
        for(i=0;i<4;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
        printf("Case #%d: %s\n",k,(judge())?"Yes":"No");
    }
    return 0;
}

 

posted on 2015-04-18 21:00  与我同在  阅读(191)  评论(0编辑  收藏  举报

导航