(CF)B. Restore 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


题意:给出8个顶点的坐标,每个顶点的x,y,z值被打乱了。问是否这8个顶点构成一个立方体,能的话就输出YES,并按照顺序输出8个顶点坐标。否则输出NO.

题解思路:首先每个顶点有6种状态,共8个点,所以枚举6的8次方个状态。用next_permutation枚举。然后进行判断。有两点重合肯定不是立方体,然后以一点到另外7点的距离是有规律的。前三个相等,中间三个相等,最后一个。3 3 1 状态。(我这里是距离的平方)。然后以两点到另外6个点构成的三角形要么是直角三角形要么是等边三角形。如果条件都满足的话那么这八个顶点肯定能组成立方体了。 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define EPS 1e-8
using namespace std;
LL a[10][4];
double dis(LL x[3],LL y[3])
{
	return (x[0]-y[0])*(x[0]-y[0])+(x[2]-y[2])*(x[2]-y[2])+(x[1]-y[1])*(x[1]-y[1]);
}
void DFS(int k)
{
	if (k!=8)
	{
		sort(a[k],a[k]+3);
		do
		{
			DFS(k+1);
		}while(next_permutation(a[k],a[k]+3));
	}
	else
	{
		double f[10];
		for (int i=1;i<8;i++)
			for (int j=i+1;j<=8;j++)
				if (dis(a[i],a[j])==0) return;
		for (int i=2;i<=8;i++)
			f[i-2]=dis(a[1],a[i]);
		sort(f,f+7);
		int flag=1;
		if (f[0]<=EPS) return; 
		if (f[0]==f[1] && f[1]==f[2] && f[3]==f[4] && f[4]==f[5] && 2*f[2]==f[3] && f[6]==f[5]+f[0])
			flag=0;
		if (flag==1) return;
		double g[3];
		g[0]=dis(a[1],a[2]);
		flag=0;
		for (int i=3;i<=8;i++)
		{
			g[1]=dis(a[1],a[i]);
			g[2]=dis(a[2],a[i]);
			sort(g,g+3);
			if (g[0]+g[1]!=g[2]&& (g[0]!=g[1] && g[1]!=g[2])) 
			{
				flag=1;
				break;
			}
		}
		if (flag) return;
		puts("YES");
		for (int i=1;i<=8;i++)
			printf("%I64d %I64d %I64d\n",a[i][0],a[i][1],a[i][2]);
		exit(0);
	}
}
int main()
{
	for (int i=1;i<=8;i++)
		scanf("%I64d%I64d%I64d",&a[i][0],&a[i][1],&a[i][2]);
	DFS(1);
	puts("NO");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值