Codeforces 464B. Restore Cube (一種高效八點判斷立方體(cube)的辦法)

B. Restore Cube
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers — coordinates of cube's vertex (a single line contains coordinates of a single vertex, each vertex is written exactly once), put the paper on the table and left. While Peter was away, his little brother Nick decided to play with the numbers on the paper. In one operation Nick could swap some numbers inside a single line (Nick didn't swap numbers from distinct lines). Nick could have performed any number of such operations.

When Peter returned and found out about Nick's mischief, he started recollecting the original coordinates. Help Peter restore the original position of the points or else state that this is impossible and the numbers were initially recorded incorrectly.

Input

Each of the eight lines contains three space-separated integers — the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 106 in their absolute value.

Output

If there is a way to restore the cube, then print in the first line "YES". In each of the next eight lines print three integers — the restored coordinates of the points. The numbers in the i-th output line must be a permutation of the numbers in i-th input line. The numbers should represent the vertices of a cube with non-zero length of a side. If there are multiple possible ways, print any of them.

If there is no valid way, print "NO" (without the quotes) in the first line. Do not print anything else.

Sample test(s)
input
0 0 0
0 0 1
0 0 1
0 0 1
0 1 1
0 1 1
0 1 1
1 1 1
output
YES
0 0 0
0 0 1
0 1 0
1 0 0
0 1 1
1 0 1
1 1 0
1 1 1
input
0 0 0
0 0 0
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
1 1 1
output
NO


http://codeforces.com/contest/464/problem/B


//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN
#define N 10
#define M 200
struct Point
{
    ll x[N],y[N],z[N];
    int len;
}poi[M];
void add(int id,ll x,ll y,ll z)
{
    int len=poi[id].len;
    bool ok=true;
    repin(i,1,len)
    {
        if(x==poi[id].x[i] && y==poi[id].y[i] && z==poi[id].z[i])
        {
            ok=false;
            break;
        }
    }
    if(ok)
    {
        poi[id].len++;
        int t=poi[id].len;
        poi[id].x[t]=x;
        poi[id].y[t]=y;
        poi[id].z[t]=z;
    }
}
struct Points
{
    ll x,y,z;
}p[M];
ll dis[M];
int num;
ll Getdistance(ll xx1,ll yy1,ll zz1,ll xx2,ll yy2,ll zz2)
{
    ll t1=xx1-xx2;
    ll t2=yy1-yy2;
    ll t3=zz1-zz2;
    return t1*t1+t2*t2+t3*t3;
}
bool cube()
{
    bool alldifferent=true;
    repin(i,1,8)
    {
        if(!alldifferent) break;
        repin(j,1,8)
        {
            if(i==j) continue;
            if(p[i].x==p[j].x && p[i].y==p[j].y && p[i].z==p[j].z)
            {
                alldifferent=false;
                break;
            }
        }
    }
    if(!alldifferent) return false;
    ll first;
    repin(i,1,8)
    {
        num=0;
        repin(j,1,8)
        {
            if(i==j) continue;
            num++;
            dis[num]=Getdistance(p[i].x,p[i].y,p[i].z,p[j].x,p[j].y,p[j].z);
        }
        sort(dis+1,dis+1+7);
        first=dis[1];
        repin(i,1,7)
        {
            if(i<=3)
            {
                if(dis[i]!=first) return false;
            }
            else if(i>=4 && i<=6)
            {
                if(dis[i]!=2*first) return false;
            }
            else if(i==7)
            {
                if(dis[i]!=3*first) return false;
            }
        }
    }
    return true;
}
void dfs(int last,int sum)
{
    if(sum==8)
    {
        if(cube())
        {
            printf("YES\n");
            repin(i,1,8)
            {
                printf("%lld %lld %lld\n",p[i].x,p[i].y,p[i].z);
            }
            exit(0);
        }
        return;
    }
    int now=last+1;
    int len=poi[now].len;
    repin(i,1,len)
    {
        p[now].x=poi[now].x[i];
        p[now].y=poi[now].y[i];
        p[now].z=poi[now].z[i];
        dfs(now,sum+1);
    }
}
int main()
{
//    input;
    ll x,y,z;
    repin(i,1,8)
    {
        cin>>x>>y>>z;
        poi[i].len=0;
        //6種情況
        add(i,x,y,z);
        add(i,x,z,y);
        add(i,y,x,z);
        add(i,y,z,x);
        add(i,z,x,y);
        add(i,z,y,x);
    }
    dfs(0,0);
    printf("NO\n");
}


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