codeforces 265 D. Restore Cube

本文介绍了一种通过已知经过数字交换后的正方体顶点坐标来恢复原始位置的方法。利用排列组合与距离判断技术,确保恢复的坐标能够构成正确的正方体结构。

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

D. 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

题目大意:
正方体8个点,xyz坐标可换,枚举6^8,每种判断一个店到另外7个点得长度满足正方体约束
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
struct Node
{
    int x[5];
    void sort1()
    {
        sort(x, x + 3);
    }
};
Node nn[10];
bool init()
{
    int i = 0;
    while (scanf("%d%d%d", &nn[i].x[0], &nn[i].x[1], &nn[i].x[2]) + 1)
    {
        i++;
        if (i == 8)
            break;
    }
    if (i == 8)
    {
        for (int j = 0; j < 8; j++)
        {
            nn[j].sort1();
        }
        return true;
    }
    else
        return false;
}
LL dis(Node a,Node b)
{
    long long ret=0;
    long long x=a.x[0]-b.x[0];
    long long y=a.x[1]-b.x[1];
    long long z=a.x[2]-b.x[2];
    ret=x*x+y*y+z*z;
    return ret;
}
bool check()
{
    long long dd[100]=zero;
    int num=0;
    for(int i=0;i<8;i++)
    {
        num=0;
        for(int j=0;j<8;j++)
        {
            if(i==j)
                continue;
            dd[num++]=dis(nn[i],nn[j]);
        }
        sort(dd,dd+num);
        long long d1=dd[0],d2=dd[0]*2,d3=dd[0]*3;
        if(d1==0||d1!=dd[1]||d1!=dd[2]||d2!=dd[3]||d2!=dd[4]||d2!=dd[5]||d3!=dd[6])
            return false;
    }
    return true;
}
int ans = 0;
bool dfs(int k)
{
    if (k == 8)
    {
        if (check())
            return true;
        return false;
    }
    do
    {
        if (dfs(k + 1))
            return true;
    }
    while (next_permutation(nn[k].x, nn[k].x + 3));
    return false;
}
int main()
{
#ifdef DeBUGs
    freopen("1.in", "r", stdin);
#endif
    while (init())
    {
        if (dfs(0))
        {
            printf("YES\n");
            for (int i = 0; i < 8; i++)
            {
                printf("%d %d %d\n", nn[i].x[0], nn[i].x[1], nn[i].x[2]);
            }
        }
        else
            printf("NO\n");
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值