Advertisement
Rubykuby

Backtracking

Apr 19th, 2014
3,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. // Exercise description:
  7. // The numbers 1-8 are distributed over a cube with the corners a, b, c, d, e, f, g, h.
  8. // The sum of the corners of every surface must be 18.
  9. // Write a method in C# - using backtracking - to create a list of all possible cube configurations.
  10.  
  11. namespace TentProg3 {
  12.     class Program {
  13.         public static List<List<int>> magicCube() {
  14.             List<List<int>> cubes = new List<List<int>>();
  15.             for (int a = 1; a < 9; a++) {
  16.                 for (int b = 1; b < 9; b++) {
  17.                     if (a != b) {
  18.                         for (int c = 1; c < 9; c++) {
  19.                             if ((c != a) && (c != b)) {
  20.                                 for (int d = 1; d < 9; d++) {
  21.                                     if ((d != a) && (d != b) && (d != c)) {
  22.                                         if (a + b + c + d != 18)
  23.                                             continue;
  24.                                         for (int e = 1; e < 9; e++) {
  25.                                             if ((e != a) && (e != b) && (e != c) && (e != d)) {
  26.                                                 for (int f = 1; f < 9; f++) {
  27.                                                     if ((f != a) && (f != b) && (f != c) && (f != d) && (f != e)) {
  28.                                                         if (a + b + e + f != 18)
  29.                                                             continue;
  30.                                                         for (int g = 1; g < 9; g++) {
  31.                                                             if ((g != a) && (g != b) && (g != c) && (g != d) && (g != e) && (g != f)) {
  32.                                                                 if (b + c + f + g != 18)
  33.                                                                     continue;
  34.                                                                 for (int h = 1; h < 9; h++) {
  35.                                                                     if ((h != a) && (h != b) && (h != c) && (h != d) && (h != e) && (h != f) && (h != g)) {
  36.                                                                         if ((e + f + g + h != 18) && (a + d + h + e != 18) && (c + d + h + g != 18))
  37.                                                                             continue;
  38.                                                                         List<int> cube = new List<int>();
  39.                                                                         cube.Add(a);
  40.                                                                         cube.Add(b);
  41.                                                                         cube.Add(c);
  42.                                                                         cube.Add(d);
  43.                                                                         cube.Add(e);
  44.                                                                         cube.Add(f);
  45.                                                                         cube.Add(g);
  46.                                                                         cube.Add(h);
  47.                                                                         cubes.Add(cube);
  48.                                                                     }
  49.                                                                 }
  50.                                                             }
  51.                                                         }
  52.                                                     }
  53.                                                 }
  54.                                             }
  55.                                         }
  56.                                     }
  57.                                 }
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             return cubes;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement