psotirov

NMShortestReach

May 31st, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4.  
  5. namespace _10_Shortest_reach
  6. {
  7.     public class CustomLinkedList<T> where T : IComparable
  8.     {
  9.         private T[] values;
  10.         private int[] parents;
  11.         private SortedSet<T> sortedValues; // this member is used only to increase the searching speed from O(n) to O(ln n)
  12.         public int Count { get; private set; }
  13.  
  14.         public CustomLinkedList()
  15.         {
  16.             this.values = new T[4];
  17.             this.parents = new int[4];
  18.             this.sortedValues = new SortedSet<T>();
  19.             this.Count = 0;
  20.         }
  21.  
  22.         public T this[int index]
  23.         {
  24.             get
  25.             {
  26.                 if (index < 0 || index >= this.Count)
  27.                 {
  28.                     throw new IndexOutOfRangeException();
  29.                 }
  30.  
  31.                 return this.values[index];
  32.             }
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Returns the index of the parent element (i.e. upper level of the tree)
  37.         /// </summary>
  38.         public int GetParent(int index)
  39.         {
  40.             if (index < 0 || index >= this.Count)
  41.             {
  42.                 throw new IndexOutOfRangeException();
  43.             }
  44.  
  45.             return this.parents[index];
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Adds new value to the queue
  50.         /// </summary>
  51.         /// <param name="parent">index of the parent element (i.e. upper level of the tree)</param>
  52.         public void Add(T value, int parent = -1)
  53.         {
  54.             if (this.Count == this.values.Length)
  55.             {
  56.                 T[] newValues = new T[this.values.Length * 2];
  57.                 this.values.CopyTo(newValues, 0);
  58.                 this.values = newValues;
  59.  
  60.                 int[] newParents = new int[this.parents.Length * 2];
  61.                 this.parents.CopyTo(newParents, 0);
  62.                 this.parents = newParents;
  63.             }
  64.  
  65.             this.values[this.Count] = value;
  66.             this.parents[this.Count] = parent;
  67.             this.sortedValues.Add(value); // it will add sucessfully only unique values
  68.             this.Count++;
  69.         }
  70.  
  71.         public bool Contains(T value)
  72.         {
  73.             // return (Array.IndexOf<T>(this.values, value) >= 0); // O(n) that results to O(n^2) for the application, very slow for numbers above 50000
  74.             return (this.sortedValues.Contains(value)); // O(ln n), better
  75.         }
  76.     }
  77.  
  78.     static class ShortestReach
  79.     {
  80.         static private CustomLinkedList<int> list = new CustomLinkedList<int>();
  81.  
  82.         static void Main()
  83.         {
  84.             Console.Write("Start number N: ");
  85.             int start = int.Parse(Console.ReadLine());
  86.  
  87.             Console.Write("End number M: ");
  88.             int end = int.Parse(Console.ReadLine());
  89.  
  90.             if (start <= 0 || end < start)
  91.             {
  92.                 throw new ArgumentOutOfRangeException("start N should be positive and less than end M");
  93.             }
  94.  
  95.             list.Add(start);
  96.             int current = list.Count-1;
  97.  
  98.             while (current < list.Count)
  99.             {
  100.                 if (list[current] == end) // solution has been found
  101.                 {
  102.                     Console.WriteLine(Environment.NewLine + "Total objects created: " + list.Count);
  103.                     Console.WriteLine(Environment.NewLine + "Solution: " + ShowResult(current));
  104.  
  105.                     Console.WriteLine("Press Enter to finish");
  106.                     Console.ReadLine();
  107.                     return;
  108.                 }
  109.  
  110.                 // for each new member checks if it is within range and it not exists already
  111.                 if (list[current] + 1 <= end && !list.Contains(list[current] + 1))
  112.                 {
  113.                     list.Add(list[current] + 1, current);
  114.                 }
  115.  
  116.                 if (list[current] + 2 <= end && !list.Contains(list[current] + 2))
  117.                 {
  118.                     list.Add(list[current] + 2, current);
  119.                 }
  120.  
  121.                 if (list[current] * 2 <= end && !list.Contains(list[current] * 2))
  122.                 {
  123.                     list.Add(list[current] * 2, current);
  124.                 }
  125.  
  126.                 current++;
  127.             }
  128.  
  129.             // this should not be reached as there is always a solution
  130.             throw new ApplicationException("No solution");
  131.         }
  132.  
  133.         /// <summary>
  134.         /// Returns the list of all values of the soluton from start to end
  135.         /// Actually it reveses the saved order from last to first using parents
  136.         /// </summary>
  137.         /// <param name="last">Last value</param>
  138.         /// <returns>string of solution path (for example "1 -> 5 -> 7 -> 8 -> 16")</returns>
  139.         static string ShowResult(int last)
  140.         {
  141.             StringBuilder output = new StringBuilder(list[last].ToString());
  142.             while (list.GetParent(last) >= 0)
  143.             {
  144.                 last = list.GetParent(last);
  145.                 output.Insert(0, list[last] + " -> ");
  146.             }
  147.  
  148.             return output.ToString();
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment