Advertisement
viraco4a

AlgorithmsCubes

Apr 2nd, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Cubes
  7. {
  8.  
  9. class Cube
  10. {
  11. public int FrontTop { get; set; }
  12. public int FrontRight { get; set; }
  13. public int FrontBottom { get; set; }
  14. public int FrontLeft { get; set; }
  15.  
  16. public int BackTop { get; set; }
  17. public int BackRight { get; set; }
  18. public int BackBottom { get; set; }
  19. public int BackLeft { get; set; }
  20.  
  21. public int TopLeft { get; set; }
  22. public int TopRight { get; set; }
  23. public int BottomLeft { get; set; }
  24. public int BottomRight { get; set; }
  25. }
  26.  
  27. class Program
  28. {
  29.  
  30. private static int[] colors;
  31. private static HashSet<string> result = new HashSet<string>();
  32. private static HashSet<string> allPossibleCubes = new HashSet<string>();
  33.  
  34.  
  35. static void Main()
  36. {
  37. var input = Console.ReadLine()
  38. .Split(' ')
  39. .Select(s => int.Parse(s))
  40. .ToArray();
  41. colors = input.OrderBy(s => s).ToArray();
  42. Permute(colors, 0, 11);
  43. Console.WriteLine(result.Count);
  44. }
  45.  
  46. private static void Permute(int[] arr, int start, int end)
  47. {
  48. MarkCube();
  49. for (int left = end - 1; left >= start; left--)
  50. {
  51. for (int right = left + 1; right <= end; right++)
  52. {
  53. if (arr[left] == arr[right]) continue;
  54.  
  55. Swap(ref arr[left], ref arr[right]);
  56. Permute(arr, left + 1, end);
  57. }
  58.  
  59. var firstElement = arr[left];
  60. for (var i = left; i <= end - 1; i++)
  61. {
  62. arr[i] = arr[i + 1];
  63. }
  64. arr[end] = firstElement;
  65. }
  66. }
  67.  
  68. static void Swap<T>(ref T first, ref T second)
  69. {
  70. var oldFirst = first;
  71. first = second;
  72. second = oldFirst;
  73. }
  74.  
  75. private static void MarkCube()
  76. {
  77. Cube newCube = new Cube()
  78. {
  79. FrontTop = colors[0],
  80. FrontRight = colors[1],
  81. FrontBottom = colors[2],
  82. FrontLeft = colors[3],
  83. BackTop = colors[4],
  84. BackRight = colors[5],
  85. BackBottom = colors[6],
  86. BackLeft = colors[7],
  87. TopLeft = colors[8],
  88. TopRight = colors[9],
  89. BottomLeft = colors[10],
  90. BottomRight = colors[11]
  91. };
  92. string cube = ConvertCubeToString(newCube);
  93.  
  94. if (allPossibleCubes.Contains(cube))
  95. {
  96. return;
  97. }
  98. result.Add(cube);
  99.  
  100. for (int az = 0; az < 3; az++)
  101. {
  102. allPossibleCubes.Add(cube);
  103. newCube = AzimuthRotate(newCube);
  104. cube = ConvertCubeToString(newCube);
  105. for (int el = 0; el < 3; el++)
  106. {
  107. allPossibleCubes.Add(cube);
  108. newCube = ElevationRotate(newCube);
  109. cube = ConvertCubeToString(newCube);
  110. for (int roll = 0; roll < 4; roll++)
  111. {
  112. allPossibleCubes.Add(cube);
  113. newCube = RollRotate(newCube);
  114. cube = ConvertCubeToString(newCube);
  115. }
  116. }
  117. }
  118.  
  119. }
  120.  
  121. private static string ConvertCubeToString(Cube cube)
  122. {
  123. StringBuilder sb = new StringBuilder();
  124. sb.Append(cube.FrontTop);
  125. sb.Append(cube.FrontRight);
  126. sb.Append(cube.FrontLeft);
  127. sb.Append(cube.FrontBottom);
  128. sb.Append(cube.BackTop);
  129. sb.Append(cube.BackRight);
  130. sb.Append(cube.BackLeft);
  131. sb.Append(cube.BackBottom);
  132. sb.Append(cube.TopRight);
  133. sb.Append(cube.TopLeft);
  134. sb.Append(cube.BottomRight);
  135. sb.Append(cube.BottomLeft);
  136. return sb.ToString();
  137. }
  138.  
  139. private static Cube AzimuthRotate(Cube cube)
  140. {
  141. var newCube = new Cube()
  142. {
  143. FrontTop = cube.TopRight,
  144. TopLeft = cube.FrontTop,
  145. BackTop = cube.TopLeft,
  146. TopRight = cube.BackTop,
  147. FrontLeft = cube.FrontRight,
  148. BackLeft = cube.FrontLeft,
  149. BackRight = cube.BackLeft,
  150. FrontRight = cube.BackRight,
  151. FrontBottom = cube.BottomRight,
  152. BottomLeft = cube.FrontBottom,
  153. BackBottom = cube.BottomLeft,
  154. BottomRight = cube.BackBottom
  155. };
  156.  
  157. return newCube;
  158. }
  159.  
  160. private static Cube ElevationRotate(Cube cube)
  161. {
  162. var newCube = new Cube()
  163. {
  164. FrontTop = cube.BackTop,
  165. FrontBottom = cube.FrontTop,
  166. BackBottom = cube.FrontBottom,
  167. BackTop = cube.BackBottom,
  168. FrontLeft = cube.TopLeft,
  169. BottomLeft = cube.FrontLeft,
  170. BackLeft = cube.BottomLeft,
  171. TopLeft = cube.BackLeft,
  172. FrontRight = cube.TopRight,
  173. BottomRight = cube.FrontRight,
  174. BackRight = cube.BottomRight,
  175. TopRight = cube.BackRight
  176. };
  177.  
  178. return newCube;
  179. }
  180.  
  181. private static Cube RollRotate(Cube cube)
  182. {
  183. var newCube = new Cube()
  184. {
  185. FrontTop = cube.FrontRight,
  186. FrontLeft = cube.FrontTop,
  187. FrontBottom = cube.FrontLeft,
  188. FrontRight = cube.FrontBottom,
  189. BackTop = cube.BackRight,
  190. BackLeft = cube.BackTop,
  191. BackBottom = cube.BackLeft,
  192. BackRight = cube.BackBottom,
  193. TopLeft = cube.TopRight,
  194. BottomLeft = cube.TopLeft,
  195. BottomRight = cube.BottomLeft,
  196. TopRight = cube.BottomRight
  197. };
  198.  
  199. return newCube;
  200. }
  201.  
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement