Advertisement
aslv

Sorting

Mar 7th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _5Sorting
  7. {
  8.     class PermNodeComparer : IEqualityComparer<int[]>
  9.     {
  10.         public bool Equals(int[] x, int[] y)
  11.         {
  12.             return x.SequenceEqual(y);
  13.         }
  14.  
  15.         public int GetHashCode(int[] obj)
  16.         {
  17.             if (obj != null)
  18.             {
  19.                 unchecked
  20.                 {
  21.                     int hashCode = 17;
  22.                     obj.ToList().ForEach(el => hashCode = hashCode * 23 + el.GetHashCode());
  23.                     return hashCode;
  24.                 }
  25.             }
  26.             return 0;
  27.         }
  28.     }
  29.  
  30.     class PermNode
  31.     {
  32.         public int Steps { get; set; }
  33.         public int[] A { get; set; }
  34.  
  35.         public PermNode(int steps, int[] a)
  36.         {
  37.             this.Steps = steps;
  38.             this.A = a;
  39.         }
  40.     }
  41.  
  42.     public class Sorting
  43.     {
  44.         static int n, k;
  45.         static Dictionary<int[], List<int[]>> graph = new Dictionary<int[], List<int[]>>(new PermNodeComparer());
  46.         static Dictionary<int[], bool> visited = new Dictionary<int[], bool>(new PermNodeComparer());
  47.         static int[] arr;
  48.  
  49.         public static void Main()
  50.         {
  51.             n = int.Parse(Console.ReadLine());
  52.             int[] startShuffle = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  53.             k = int.Parse(Console.ReadLine());
  54.  
  55.             GenerateGraph();
  56.  
  57.             //foreach (var node in graph)
  58.             //{
  59.             //    Console.Write("({0}): {{ ", string.Join(" ", node.Key));
  60.             //    foreach (var el in node.Value)
  61.             //    {
  62.             //        Console.Write("{0}; ", string.Join(" ", el));
  63.             //    }
  64.             //    Console.WriteLine("}");
  65.             //}
  66.  
  67.             InitArrSorted();
  68.             Queue<PermNode> q = new Queue<PermNode>(graph.Count >> 1);
  69.             if (!graph.ContainsKey(startShuffle))
  70.             {
  71.                 Console.Error.WriteLine("The input sequence {{{0}}} is not a permutation of {{{1}}}!",
  72.                     string.Join(" ", startShuffle), string.Join(" ", arr));
  73.                 return;
  74.             }
  75.             q.Enqueue(new PermNode(0, startShuffle));
  76.             PermNode current;
  77.             Console.WriteLine("Calculating . . .");
  78.             while (q.Count > 0)
  79.             {
  80.                 current = q.Dequeue();
  81.                 visited[current.A] = true;
  82.                 if (current.A.SequenceEqual(arr))
  83.                 {
  84.                     Console.WriteLine(current.Steps);
  85.                     return;
  86.                 }
  87.                 foreach (var node in graph[current.A])
  88.                 {
  89.                     if (!visited[node])
  90.                     {
  91.                         q.Enqueue(new PermNode(current.Steps + 1, node));
  92.                     }
  93.                 }
  94.             }
  95.             Console.WriteLine(-1);
  96.         }
  97.  
  98.         static void InitArrSorted()
  99.         {
  100.             arr = new int[n];
  101.             for (int i = 0; i < n; i++)
  102.             {
  103.                 arr[i] = i + 1;
  104.             }
  105.         }
  106.  
  107.         static void GenerateGraph()
  108.         {
  109.             InitArrSorted();
  110.  
  111.             Permute(0);
  112.  
  113.             int[] a;
  114.             foreach (var key in graph.Keys)
  115.             {
  116.                 for (int j = 0; j <= n - k; j++)
  117.                 {
  118.                     Array.Copy(key, arr, n);
  119.                     Array.Reverse(arr, j, k);
  120.                     a = new int[n];
  121.                     Array.Copy(arr, a, n);
  122.                     graph[key].Add(a);
  123.                 }
  124.             }
  125.         }
  126.  
  127.         static void Permute(int k)
  128.         {
  129.             if (k == n - 1)
  130.             {
  131.                 AddToDict();
  132.             }
  133.             for (int i = k; i < n; i++)
  134.             {
  135.                 Swap(ref arr[k], ref arr[i]);
  136.                 Permute(k + 1);
  137.                 Swap(ref arr[k], ref arr[i]);
  138.             }
  139.         }
  140.  
  141.         static void AddToDict()
  142.         {
  143.             int[] a = new int[n];
  144.             Array.Copy(arr, a, n);
  145.             graph[a] = new List<int[]>();
  146.             visited[a] = false;
  147.         }
  148.  
  149.         static void Swap(ref int lhs, ref int rhs)
  150.         {
  151.             int t = lhs;
  152.             lhs = rhs;
  153.             rhs = t;
  154.            
  155.             //lhs += rhs;
  156.             //rhs = lhs - rhs;
  157.             //lhs -= rhs;
  158.  
  159.             //lhs ^= rhs;
  160.             //rhs ^= lhs;
  161.             //lhs ^= rhs;
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement