Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.98 KB | None | 0 0
  1. namespace _08.Cube
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     class CubeSolution
  8.     {
  9.         // partial solution - 20pts in judge
  10.         static HashSet<int[]> cubeCombinations = new HashSet<int[]>();
  11.         private static int output = 1;
  12.         static void Main(string[] args)
  13.         {
  14.  
  15.             int[] input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  16.            
  17.             cubeCombinations = GenerateAllCubeRotations(input);
  18.  
  19.             GenerateAllPermutations(input, 0);
  20.             Console.WriteLine(output);
  21.  
  22.  
  23.         }
  24.  
  25.         private static HashSet<int[]> GenerateAllCubeRotations(int[] input)
  26.         {
  27.             var allCubeCombinations = new HashSet<int[]>();
  28.             var cube = new Cube(input);
  29.  
  30.             for (int i = 0; i < 6; i++)
  31.             {
  32.                 if (i < 4)
  33.                 {
  34.                     cube.FlipFrontCubesToRight();
  35.                 }
  36.                 else if (i == 4)
  37.                 {
  38.                     cube.FlipFrontCubesUp();
  39.                 }
  40.                 else
  41.                 {
  42.                     cube.FlipFrontCubesUp();
  43.                     cube.FlipFrontCubesUp();
  44.                 }
  45.                 for (int j = 0; j < 4; j++)
  46.                 {
  47.                     cube.RotateCube();
  48.                     allCubeCombinations.Add(cube.GetCubeStics());
  49.                 }
  50.             }
  51.             return allCubeCombinations;
  52.         }
  53.  
  54.         static void GenerateAllPermutations(int[] arr, int k)
  55.         {
  56.             if (k >= arr.Length)
  57.             {
  58.                 bool areEqual = CompareCubes(arr);
  59.  
  60.                 if (!areEqual)
  61.                 {
  62.                     output++;
  63.                 }
  64.  
  65.             }
  66.             else
  67.             {
  68.                 var swapped = new HashSet<int>();
  69.                 for (int i = k; i < arr.Length; i++)
  70.                 {
  71.  
  72.                     if (!swapped.Contains(arr[i]))
  73.                     {
  74.                         Swap(ref arr[k], ref arr[i]);
  75.                         GenerateAllPermutations(arr, k + 1);
  76.                         Swap(ref arr[k], ref arr[i]);
  77.  
  78.                         swapped.Add(arr[i]);
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.         private static bool CompareCubes(int[] arr)
  85.         {
  86.             var arrayCubeRotations = GenerateAllCubeRotations(arr);
  87.  
  88.             foreach (var cubeCombination in cubeCombinations)
  89.             {
  90.                 foreach (var arrayCubeRotation in arrayCubeRotations)
  91.                 {
  92.                     if (cubeCombination.SequenceEqual(arrayCubeRotation))
  93.                     {
  94.                         return true;
  95.                     }
  96.                 }
  97.             }
  98.  
  99.             return false;
  100.         }
  101.  
  102.         static void Swap(ref int first, ref int second)
  103.         {
  104.             int oldFirst = first;
  105.             first = second;
  106.             second = oldFirst;
  107.         }
  108.  
  109.         static int[] ShiftRight(int[] arr)
  110.         {
  111.             int[] shifted = new int[arr.Length];
  112.             for (int i = 0; i < arr.Length; i++)
  113.             {
  114.                 shifted[i] = arr[i == 0 ? (arr.Length - 1) : (i - 1)];
  115.             }
  116.             return shifted;
  117.         }
  118.  
  119.  
  120.         internal class Cube
  121.         {
  122.             const int edges = 12;
  123.  
  124.             public Square FrontSquare { get; set; }
  125.             public Square UpSquare { get; set; }
  126.             public Square RightSquare { get; set; }
  127.             public Square LEftSquare { get; set; }
  128.             public Square DownSquare { get; set; }
  129.             public Square BackSquare { get; set; }
  130.  
  131.             public Cube(int[] cubeValues)
  132.             {
  133.                 this.FrontSquare = new Square(cubeValues[0], cubeValues[1], cubeValues[2], cubeValues[3]);
  134.                 this.UpSquare = new Square(cubeValues[4], cubeValues[5], cubeValues[6], cubeValues[1]);
  135.                 this.RightSquare = new Square(cubeValues[2], cubeValues[6], cubeValues[7], cubeValues[8]);
  136.                 this.LEftSquare = new Square(cubeValues[9], cubeValues[4], cubeValues[0], cubeValues[10]);
  137.                 this.DownSquare = new Square(cubeValues[10], cubeValues[3], cubeValues[8], cubeValues[11]);
  138.                 this.BackSquare = new Square(cubeValues[9], cubeValues[11], cubeValues[7], cubeValues[5]);
  139.  
  140.             }
  141.  
  142.  
  143.             public void RotateCube()
  144.             {
  145.                 this.FrontSquare.RotateSquareOpposite();
  146.                 this.BackSquare.RotateSquare();
  147.                 this.FlipSideCubeSidesToRight();
  148.             }
  149.  
  150.             public override string ToString()
  151.             {
  152.                 var sticks = this.GetCubeStics();
  153.                 return String.Join(" ", sticks);
  154.             }
  155.  
  156.             public void FlipFrontCubesToRight()
  157.             {
  158.                 var spare = this.FrontSquare;
  159.                 this.FrontSquare = this.LEftSquare;
  160.                 this.LEftSquare = this.BackSquare;
  161.                 this.LEftSquare.RotateSquare();
  162.                 this.LEftSquare.RotateSquare();
  163.  
  164.                 this.BackSquare = this.RightSquare;
  165.                 this.BackSquare.RotateSquare();
  166.                 this.BackSquare.RotateSquare();
  167.                 this.RightSquare = spare;
  168.  
  169.                 this.UpSquare.RotateSquare();
  170.                 this.DownSquare.RotateSquareOpposite();
  171.             }
  172.  
  173.             public void FlipFrontCubesUp()
  174.             {
  175.                 var spare = this.FrontSquare;
  176.                 this.FrontSquare = this.UpSquare;
  177.                 this.UpSquare = this.BackSquare;
  178.                 this.BackSquare = this.DownSquare;
  179.                 this.DownSquare = spare;
  180.  
  181.                 this.LEftSquare.RotateSquareOpposite();
  182.                 this.RightSquare.RotateSquare();
  183.             }
  184.  
  185.             public void FlipSideCubeSidesToRight()
  186.             {
  187.                 var spareSquare = this.UpSquare;
  188.                 this.UpSquare = this.LEftSquare;
  189.                 this.UpSquare.RotateSquareOpposite();
  190.                 this.LEftSquare = this.DownSquare;
  191.                 this.LEftSquare.RotateSquareOpposite();
  192.                 this.DownSquare = this.RightSquare;
  193.                 this.DownSquare.RotateSquareOpposite();
  194.                 this.RightSquare = spareSquare;
  195.                 this.RightSquare.RotateSquareOpposite();
  196.             }
  197.  
  198.             public int[] GetCubeStics()
  199.             {
  200.  
  201.                 var result = new int[12];
  202.                 result[0] = this.FrontSquare.a;
  203.                 result[1] = this.FrontSquare.b;
  204.                 result[2] = this.FrontSquare.c;
  205.                 result[3] = this.FrontSquare.d;
  206.                 result[4] = this.UpSquare.a;
  207.                 result[5] = this.UpSquare.b;
  208.                 result[6] = this.UpSquare.c;
  209.                 result[7] = this.RightSquare.c;
  210.                 result[8] = this.RightSquare.d;
  211.                 result[9] = this.LEftSquare.a;
  212.                 result[10] = this.LEftSquare.d;
  213.                 result[11] = this.BackSquare.b;
  214.                 return result;
  215.             }
  216.         }
  217.  
  218.  
  219.         internal class Square
  220.         {
  221.             public int a { get; set; }
  222.             public int b { get; set; }
  223.             public int c { get; set; }
  224.             public int d { get; set; }
  225.  
  226.  
  227.             public Square(int a, int b, int c, int d)
  228.             {
  229.                 this.a = a;
  230.                 this.b = b;
  231.                 this.c = c;
  232.                 this.d = d;
  233.             }
  234.  
  235.             public void RotateSquare()
  236.             {
  237.                 var spareA = this.a;
  238.  
  239.                 this.a = this.b;
  240.                 this.b = this.c;
  241.                 this.c = this.d;
  242.                 this.d = spareA;
  243.             }
  244.  
  245.             public void RotateSquareOpposite()
  246.             {
  247.                 var spareA = this.a;
  248.  
  249.                 this.a = this.d;
  250.                 this.d = this.c;
  251.                 this.c = this.b;
  252.                 this.b = spareA;
  253.             }
  254.         }
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement