Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _5Sorting
- {
- class PermNodeComparer : IEqualityComparer<int[]>
- {
- public bool Equals(int[] x, int[] y)
- {
- return x.SequenceEqual(y);
- }
- public int GetHashCode(int[] obj)
- {
- if (obj != null)
- {
- unchecked
- {
- int hashCode = 17;
- obj.ToList().ForEach(el => hashCode = hashCode * 23 + el.GetHashCode());
- return hashCode;
- }
- }
- return 0;
- }
- }
- class PermNode
- {
- public int Steps { get; set; }
- public int[] A { get; set; }
- public PermNode(int steps, int[] a)
- {
- this.Steps = steps;
- this.A = a;
- }
- }
- public class Sorting
- {
- static int n, k;
- static Dictionary<int[], List<int[]>> graph = new Dictionary<int[], List<int[]>>(new PermNodeComparer());
- static Dictionary<int[], bool> visited = new Dictionary<int[], bool>(new PermNodeComparer());
- static int[] arr;
- public static void Main()
- {
- n = int.Parse(Console.ReadLine());
- int[] startShuffle = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- k = int.Parse(Console.ReadLine());
- GenerateGraph();
- //foreach (var node in graph)
- //{
- // Console.Write("({0}): {{ ", string.Join(" ", node.Key));
- // foreach (var el in node.Value)
- // {
- // Console.Write("{0}; ", string.Join(" ", el));
- // }
- // Console.WriteLine("}");
- //}
- InitArrSorted();
- Queue<PermNode> q = new Queue<PermNode>(graph.Count >> 1);
- if (!graph.ContainsKey(startShuffle))
- {
- Console.Error.WriteLine("The input sequence {{{0}}} is not a permutation of {{{1}}}!",
- string.Join(" ", startShuffle), string.Join(" ", arr));
- return;
- }
- q.Enqueue(new PermNode(0, startShuffle));
- PermNode current;
- Console.WriteLine("Calculating . . .");
- while (q.Count > 0)
- {
- current = q.Dequeue();
- visited[current.A] = true;
- if (current.A.SequenceEqual(arr))
- {
- Console.WriteLine(current.Steps);
- return;
- }
- foreach (var node in graph[current.A])
- {
- if (!visited[node])
- {
- q.Enqueue(new PermNode(current.Steps + 1, node));
- }
- }
- }
- Console.WriteLine(-1);
- }
- static void InitArrSorted()
- {
- arr = new int[n];
- for (int i = 0; i < n; i++)
- {
- arr[i] = i + 1;
- }
- }
- static void GenerateGraph()
- {
- InitArrSorted();
- Permute(0);
- int[] a;
- foreach (var key in graph.Keys)
- {
- for (int j = 0; j <= n - k; j++)
- {
- Array.Copy(key, arr, n);
- Array.Reverse(arr, j, k);
- a = new int[n];
- Array.Copy(arr, a, n);
- graph[key].Add(a);
- }
- }
- }
- static void Permute(int k)
- {
- if (k == n - 1)
- {
- AddToDict();
- }
- for (int i = k; i < n; i++)
- {
- Swap(ref arr[k], ref arr[i]);
- Permute(k + 1);
- Swap(ref arr[k], ref arr[i]);
- }
- }
- static void AddToDict()
- {
- int[] a = new int[n];
- Array.Copy(arr, a, n);
- graph[a] = new List<int[]>();
- visited[a] = false;
- }
- static void Swap(ref int lhs, ref int rhs)
- {
- int t = lhs;
- lhs = rhs;
- rhs = t;
- //lhs += rhs;
- //rhs = lhs - rhs;
- //lhs -= rhs;
- //lhs ^= rhs;
- //rhs ^= lhs;
- //lhs ^= rhs;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement