cf 464 Restore Cube

題目連接:http://codeforces.com/contest/464/problem/B

題目描述:給你8組(x,y,z),問能不能通過 x, y, z 之間的交換使得這8個點組成一個立方體!!!!

解題思路:題解上寫得是,先暴力所有排列 (3!)^8 ,然後check,check每個頂點到其餘頂點的距離,如果是 l, sqrt(2)*l, sqrt(3)*l,且l 不爲0 

但是我比賽的時候居然害怕double 轉 int 導致精度誤差,然後再最後+eps!!!(這是個傻X的舉動),這樣正數沒問題,但是對於-2 ,就順利的變成-1.。。。。。

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define ll long long
#define db double
#define PB push_back
using namespace std;

const int N = 100005;
const db eps = 1e-7;
const db INF = 1e50;

int p[6][3] =
{
    0, 1, 2,
    0, 2, 1,
    1, 0, 2,
    1, 2, 0,
    2, 0, 1,
    2, 1, 0
};

struct Point
{
    db x[3];
    Point (db a=0,db b=0,db c=0){x[0]=a,x[1]=b,x[2]=c;}
    void input()
    {
        int t;
        for(int i = 0; i < 3; i++) scanf("%d", &t),x[i]=t;
    }
    Point operator - (const Point &t) const
    {
        return Point(x[0]-t.x[0],x[1]-t.x[1],x[2]-t.x[2]);
    }
    int operator * (const Point &t) const
    {
        int res=0;
        for(int i=0;i<3;i++) res+=x[0]*t.x[0];
        return res;
    }
} a[8], b[8], c[8];

db dist(Point i,Point j)
{
    return sqrt((i.x[0]-j.x[0])*(i.x[0]-j.x[0])+
                (i.x[1]-j.x[1])*(i.x[1]-j.x[1])+
                (i.x[2]-j.x[2])*(i.x[2]-j.x[2]));
}

int sgn(db t)
{
    return t<-eps?-1:t>eps;
}

db dis[100];
bool check()
{
    for(int i=0;i<8;i++)
    {
        int cnt=0;
        for(int j=0;j<8;j++)
        {
            dis[cnt++]=dist(b[i],b[j]);
        }
        sort(dis,dis+cnt);
        if(sgn(dis[0])!=0||sgn(dis[1])==0||sgn(dis[1]-dis[2])!=0
           ||sgn(dis[2]-dis[3])!=0||sgn(dis[4]-dis[3]*sqrt(2.0))!=0||
           sgn(dis[4]-dis[5])!=0||sgn(dis[6]-dis[5])!=0||
           sgn(dis[7]-dis[1]*sqrt(3.0))!=0) return false;

    }
    return true;
}

bool ok;
void dfs(int k)
{
    if(k == 8)
    {
        if(check())
            ok = true;
    }
    else
    {
        for(int i = 0; i < 6; i++)
        {
            for(int j = 0; j < 3; j++) b[k].x[j] = a[k].x[p[i][j]];

            dfs(k + 1);

            if(ok) return;
        }
    }
}

int main()
{
#ifdef PKWV
    freopen("in.in", "r", stdin);
#endif // PKWV

    for(int i = 0; i < 8; i++) a[i].input();

    ok = false;
    dfs(0);
    if(ok)
    {
        printf("YES\n");
        for(int i=0;i<8;i++)
            for(int j=0;j<3;j++) printf("%d%c",(int)(b[i].x[j]),j==2?'\n':' ');
    }else printf("NO\n");
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章