using System ;

using System.Collections.Generic ;

using System.Linq ;

using System.Text ;

// Exercise description:

// The numbers 1-8 are distributed over a cube with the corners a, b, c, d, e, f, g, h.

// The sum of the corners of every surface must be 18.

// Write a method in C# - using backtracking - to create a list of all possible cube configurations.

namespace TentProg3 {

class Program {

public static List < List < int >> magicCube ( ) {

List < List < int >> cubes = new List < List < int >> ( ) ;

for ( int a = 1 ; a < 9 ; a ++ ) {

for ( int b = 1 ; b < 9 ; b ++ ) {

if ( a != b ) {

for ( int c = 1 ; c < 9 ; c ++ ) {

if ( ( c != a ) && ( c != b ) ) {

for ( int d = 1 ; d < 9 ; d ++ ) {

if ( ( d != a ) && ( d != b ) && ( d != c ) ) {

if ( a + b + c + d != 18 )

continue ;

for ( int e = 1 ; e < 9 ; e ++ ) {

if ( ( e != a ) && ( e != b ) && ( e != c ) && ( e != d ) ) {

for ( int f = 1 ; f < 9 ; f ++ ) {

if ( ( f != a ) && ( f != b ) && ( f != c ) && ( f != d ) && ( f != e ) ) {

if ( a + b + e + f != 18 )

continue ;

for ( int g = 1 ; g < 9 ; g ++ ) {

if ( ( g != a ) && ( g != b ) && ( g != c ) && ( g != d ) && ( g != e ) && ( g != f ) ) {

if ( b + c + f + g != 18 )

continue ;

for ( int h = 1 ; h < 9 ; h ++ ) {

if ( ( h != a ) && ( h != b ) && ( h != c ) && ( h != d ) && ( h != e ) && ( h != f ) && ( h != g ) ) {

if ( ( e + f + g + h != 18 ) && ( a + d + h + e != 18 ) && ( c + d + h + g != 18 ) )

continue ;

List < int > cube = new List < int > ( ) ;

cube . Add ( a ) ;

cube . Add ( b ) ;

cube . Add ( c ) ;

cube . Add ( d ) ;

cube . Add ( e ) ;

cube . Add ( f ) ;

cube . Add ( g ) ;

cube . Add ( h ) ;

cubes . Add ( cube ) ;

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

return cubes ;

}

}