Advertisement
Guest User

07.Cubes

a guest
Oct 2nd, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.03 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4.  
  5. namespace _07.Cubes
  6. {
  7.     public class Cubes
  8.     {
  9.         const int edges = 12;
  10.         //static CubeSet<Cube> cubes = new CubeSet<Cube>();
  11.         static HashSet<Cube> cubes1 = new HashSet<Cube>();
  12.         public static int allCubes = 0;
  13.  
  14.         static void Main()
  15.         {
  16.             //int[] set = new int[edges] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
  17.             //int[] set = new int[edges] { 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3 };
  18.             int[] set = ReadInput();
  19.             int uniqueEdgeConfiguration = FeedEdgeColors(set);
  20.             Console.WriteLine(uniqueEdgeConfiguration);
  21.         }
  22.  
  23.         static int[] ReadInput()
  24.         {
  25.             int[] sticks = new int[edges];
  26.             string[] sticksString = Console.ReadLine().Split(' ');
  27.             for (int i = 0; i < edges; i++)
  28.             {
  29.                 sticks[i] = int.Parse(sticksString[i]);
  30.             }
  31.  
  32.             return sticks;
  33.         }
  34.  
  35.         public static int FeedEdgeColors(int[] set)
  36.         {
  37.             //cubes.Clear();
  38.             allCubes = 0;
  39.             OccurrencesCounter<int> setCounter = new OccurrencesCounter<int>();
  40.             for (var i = 0; i < set.Length; i++)
  41.             {
  42.                 setCounter.Add(set[i]);
  43.             }
  44.             int[] arr = new int[set.Length];
  45.             bool[] busy = new bool[set.Length];
  46.             permute(setCounter, set.Length, 0, 0, -1, arr, busy);
  47.             return allCubes;
  48.             //return cubes.Count;
  49.         }
  50.  
  51.         static void permute(OccurrencesCounter<int> set, int n, int numberIndex, int numberOccurenceIndex, int numberLastIndexPlacement, int[] arr, bool[] busy)
  52.         {
  53.             if (numberIndex >= set.Count)
  54.             {
  55.                 int[] edges = new int[Cube.NUM_EDGES];
  56.                 Array.Copy(arr, edges, Cube.NUM_EDGES);
  57.                 Cube cube = new Cube(edges);
  58.                 AddCube(cube);
  59.             }
  60.             else
  61.             {
  62.                 KeyValuePair<int, int> currNumber = set[numberIndex];
  63.                 if (numberOccurenceIndex >= currNumber.Value)
  64.                 {
  65.                     permute(set, n, numberIndex + 1, 0, -1, arr, busy);
  66.                 }
  67.                 else
  68.                 {
  69.                     var start = Math.Max(numberOccurenceIndex, numberLastIndexPlacement);
  70.                     var end = n + numberOccurenceIndex - currNumber.Value + 1;
  71.                     for (var i = start; i < end; i++)
  72.                     {
  73.                         if (busy[i]) continue;
  74.                         arr[i] = currNumber.Key;
  75.                         busy[i] = true;
  76.                         permute(set, n, numberIndex, numberOccurenceIndex + 1, i, arr, busy);
  77.                         busy[i] = false;
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.  
  83.         static void AddCube(Cube cube)
  84.         {
  85.             if (cubes1.Contains(cube))
  86.             {
  87.                 return;
  88.             }
  89.             for (var i = 0; i < 4; i++)
  90.             {
  91.                 cube.RotateX();
  92.                 for (var ii = 0; ii < 4; ii++)
  93.                 {
  94.                     cube.RotateY();
  95.                     for (var iii = 0; iii < 4; iii++)
  96.                     {
  97.                         cube.RotateZ();
  98.                         cubes1.Add(new Cube(cube));
  99.                     }
  100.                 }
  101.             }
  102.             allCubes++;
  103.             //cubes.Add(cube);
  104.         }
  105.  
  106.         private static void Print(int[] array)
  107.         {
  108.             Console.WriteLine("{{ {0} }}", string.Join(", ", array));
  109.         }
  110.     }
  111.     public class Cube : IEqualityComparer<Cube>
  112.     {
  113.         public const int NUM_EDGES = 12;
  114.         public int[] edges = new int[NUM_EDGES];
  115.  
  116.         public Cube(int[] arr)
  117.         {
  118.             edges = arr;
  119.         }
  120.  
  121.         public Cube(Cube cube)
  122.         {
  123.             Array.Copy(cube.edges, edges, NUM_EDGES);
  124.         }
  125.  
  126.         public int GetHashCode(Cube obj)
  127.         {
  128.             int result = 0;
  129.             return result;
  130.         }
  131.  
  132.         public override int GetHashCode()
  133.         {
  134.             int result = GetHashCode(this);
  135.             return result;
  136.         }
  137.  
  138.         public override string ToString()
  139.         {
  140.             StringBuilder str = new StringBuilder();
  141.             str.Append(string.Join("", edges));
  142.             return str.ToString();
  143.         }
  144.  
  145.         public override bool Equals(object obj)
  146.         {
  147.             return Equals(obj as Cube);
  148.         }
  149.  
  150.         public bool Equals(Cube other)
  151.         {
  152.             bool result = Equals(this, other);
  153.             return result;
  154.         }
  155.  
  156.         public bool Equals(Cube item1, Cube item2)
  157.         {
  158.             for (int i = 0; i < Cube.NUM_EDGES; i++)
  159.             {
  160.                 if (!item1.edges[i].Equals(item2.edges[i]))
  161.                 {
  162.                     return false;
  163.                 }
  164.             }
  165.             return true;
  166.             /*
  167.             bool equal = false;
  168.             for (var i = 0; i < 4 && !equal; i++)
  169.             {
  170.                 RotateX();
  171.                 if (Equal(item1.edges, item2.edges))
  172.                 {
  173.                     equal = true;
  174.                 }
  175.                 for (var ii = 0; ii < 4 && !equal; ii++)
  176.                 {
  177.                     RotateY();
  178.                     if (Equal(item1.edges, item2.edges))
  179.                     {
  180.                         equal = true;
  181.                     }
  182.                     for (var iii = 0; iii < 4 && !equal; iii++)
  183.                     {
  184.                         RotateZ();
  185.                         if (Equal(item1.edges, item2.edges))
  186.                         {
  187.                             equal = true;
  188.                         }
  189.                     }
  190.                 }
  191.             }
  192.             return equal;
  193.              * */
  194.         }
  195.  
  196.         private bool Equal(int[] arr1, int[] arr2)
  197.         {
  198.             for (var i = 0; i < arr1.Length; i++)
  199.             {
  200.                 if (arr1[i] != arr2[i])
  201.                 {
  202.                     return false;
  203.                 }
  204.             }
  205.             return true;
  206.         }
  207.         public void RotateX()
  208.         {
  209.             int[] newEdges = new int[]
  210.             {
  211.                 edges[4], edges[3], edges[5], edges[10],
  212.                 edges[8], edges[11], edges[2], edges[0],
  213.                 edges[7], edges[1], edges[9], edges[6]
  214.             };
  215.             edges = newEdges;
  216.         }
  217.         public void RotateY()
  218.         {
  219.             int[] newEdges = new int[]
  220.             {
  221.                 edges[8], edges[7], edges[0], edges[4],
  222.                 edges[10], edges[3], edges[1], edges[9],
  223.                 edges[11], edges[6], edges[5], edges[2]
  224.             };
  225.             edges = newEdges;
  226.         }
  227.         public void RotateZ()
  228.         {
  229.             int[] newEdges = new int[]
  230.             {
  231.                 edges[1], edges[2], edges[3], edges[0],
  232.                 edges[7], edges[4], edges[5], edges[6],
  233.                 edges[9], edges[11], edges[8], edges[10]
  234.             };
  235.             edges = newEdges;
  236.         }
  237.     }
  238.  
  239.     public class CubeSet<T>
  240.     {
  241.         public CubeSet()
  242.         {
  243.             list = new List<T>();
  244.         }
  245.         public CubeSet(int cap)
  246.         {
  247.             list = new List<T>(cap);
  248.         }
  249.         public int Count
  250.         {
  251.             private set
  252.             {
  253.             }
  254.             get
  255.             {
  256.                 return list.Count;
  257.             }
  258.         }
  259.         public int Capacity
  260.         {
  261.             private set { }
  262.             get { return list.Capacity; }
  263.         }
  264.         private List<T> list;
  265.  
  266.         public void Add(T cube)
  267.         {
  268.             if (!Contains(cube))
  269.             {
  270.                 list.Add(cube);
  271.             }
  272.         }
  273.  
  274.         public void Clear()
  275.         {
  276.             list.Clear();
  277.         }
  278.  
  279.         private bool Contains(T cube)
  280.         {
  281.             bool contains = false;
  282.             for (var i = 0; i < list.Count; i++)
  283.             {
  284.                 if (list[i].Equals(cube))
  285.                 {
  286.                     contains = true;
  287.                     break;
  288.                 }
  289.             }
  290.             return contains;
  291.         }
  292.  
  293.         public T this[int index]
  294.         {
  295.             set
  296.             {
  297.  
  298.             }
  299.             get
  300.             {
  301.                 return list[index];
  302.             }
  303.         }
  304.     }
  305.    
  306.     public class OccurrencesCounter<T>
  307.     {
  308.         public class ListNode<T>
  309.         {
  310.             public T Value { get; private set; }
  311.             public int OccurrencesCount { get; set; }
  312.             public ListNode<T> NextNode { get; set; }
  313.             public ListNode()
  314.             {
  315.             }
  316.             public ListNode(T value, ListNode<T> next)
  317.             {
  318.                 this.Value = value;
  319.                 this.NextNode = next;
  320.                 this.OccurrencesCount = 0;
  321.             }
  322.         }
  323.  
  324.         private ListNode<T> Head;
  325.         private ListNode<T> Tail;
  326.         public int Count { get; private set; }
  327.  
  328.         public void Add(T value)
  329.         {
  330.             if (this.Count == 0)
  331.             {
  332.                 var node = new ListNode<T>(value, null);
  333.                 node.OccurrencesCount++;
  334.                 this.Head = node;
  335.                 this.Tail = node;
  336.                 this.Count++;
  337.             }
  338.             else
  339.             {
  340.                 bool found = false;
  341.                 ListNode<T> temp = null;
  342.                 for (var i = 0; i < this.Count; i++)
  343.                 {
  344.                     if (i == 0) temp = this.Head;
  345.  
  346.                     if (value.Equals(temp.Value))
  347.                     {
  348.                         temp.OccurrencesCount++;
  349.                         found = true;
  350.                         break;
  351.                     }
  352.                     temp = temp.NextNode;
  353.                 }
  354.  
  355.                 if (found == false)
  356.                 {
  357.                     var node = new ListNode<T>(value, null);
  358.                     node.OccurrencesCount++;
  359.                     this.Tail.NextNode = node;
  360.                     this.Tail = node;
  361.                     this.Count++;
  362.                 }
  363.             }
  364.         }
  365.  
  366.         public Dictionary<T, int> ToDictionary()
  367.         {
  368.             Dictionary<T, int> dict = new Dictionary<T, int>();
  369.             ListNode<T> temp = null;
  370.             for (var i = 0; i < this.Count; i++)
  371.             {
  372.                 if (i == 0)
  373.                 {
  374.                     temp = this.Head;
  375.                 }
  376.                 dict[temp.Value] = temp.OccurrencesCount;
  377.                 temp = temp.NextNode;
  378.             }
  379.             return dict;
  380.         }
  381.  
  382.         public KeyValuePair<T, int> this[int index]
  383.         {
  384.             get
  385.             {
  386.                 if (index >= this.Count)
  387.                 {
  388.                     throw new IndexOutOfRangeException("OccurrencesCounter indexer problem.");
  389.                 }
  390.                 ListNode<T> temp = null;
  391.                 for (var i = 0; i < this.Count; i++)
  392.                 {
  393.                     if (i == 0)
  394.                     {
  395.                         temp = this.Head;
  396.                     }
  397.                     if (i == index)
  398.                     {
  399.                         return new KeyValuePair<T, int>(temp.Value, temp.OccurrencesCount);
  400.                     }
  401.                     temp = temp.NextNode;
  402.                 }
  403.                 return new KeyValuePair<T, int>();
  404.             }
  405.         }
  406.  
  407.         public override string ToString()
  408.         {
  409.             StringBuilder str = new StringBuilder();
  410.             ListNode<T> temp = new ListNode<T>();
  411.             for (var i = 0; i < this.Count; i++)
  412.             {
  413.                 if (i == 0)
  414.                 {
  415.                     temp = this.Head;
  416.                 }
  417.                 str.Append(string.Format("{0} => {1}\n", temp.Value, temp.OccurrencesCount));
  418.                 temp = temp.NextNode;
  419.             }
  420.             return str.ToString();
  421.         }
  422.     }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement