Codeforces Round #265 (Div. 2) D. Restore Cube brute force,
D. Restore Cubetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Peter had a cube with non-zero length of a side. he put the cube into three-dimen1_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 cocould swap some numbers inside a single line (Nick didn't swap numbers from distinct lines ). nick cocould have saved med 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 inI-Th output line must be a permutation of the numbers inI-Th input line. The numbers shoshould 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 00 0 10 0 10 0 10 1 10 1 10 1 11 1 1
Output
YES0 0 00 0 10 1 01 0 00 1 11 0 11 1 01 1 1
Input
0 0 00 0 00 0 00 0 01 1 11 1 11 1 11 1 1
Output
NO
The meaning of the question is given the coordinates of the eight points, and asked if the coordinates of each point can be rearranged to form a cube. The idea is to forcibly enumerate the coordinates of each point and check whether it is valid when enumeration to the last point, the Code is as follows:
# Include <bits/stdc ++. h> using namespace std; typedef long LL; LL a [9] [5], B [8]; LL dis (int I, int j) {LL x = a [I] [0]-a [j] [0], y = a [I] [1]-a [j] [1], z = a [I] [2]-a [j] [2]; return x * x + y * y + z * z;} bool check () {for (int I = 0; I <8; I ++) {for (int j = 0; j <8; j ++) {if (I = j) continue; if (j <I) B [j] = dis (I, j); else B [J-1] = dis (I, j);} sort (B, B + 7); LL t1 = B [0], t2 = 2 * B [0], t3 = 3 * B [0]; /// check whether the three sides and three sides of the diagonal line start with I meet the square constraint if (t1 = 0 | t1! = B [1] | t1! = B [2] | t2! = B [3] | t2! = B [4] | t2! = B [5] | t3! = B [6]) return false;} return true;} bool is_ OK (int cur) {if (cur = 8) {return check ();} sort (a [cur], a [cur] + 3); do {if (is_ OK (cur + 1) return true;} while (next_permutation (a [cur], a [cur] + 3); return false;} int main () {for (int I = 0; I <8; I ++) cin> a [I] [0]> a [I] [1]> a [I] [2]; if (! Is_ OK (0) puts ("NO"); else {puts ("YES"); for (int I = 0; I <8; I ++) for (int j = 0; j <3; j ++) {cout <a [I] [j] <''; if (j = 2) cout <endl ;}} return 0 ;}