Codeforces 464B. Restore Cube (一种高效八点判断立方体(cube)的办法)

面对被小弟随意调换的立方体顶点坐标,彼得需要找出这些坐标原本的排列方式以还原立方体的位置。此问题涉及算法设计与几何空间理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值