Advertisement
ArXen42

Поиск цепочки возрастания, вариант на ссылках

Mar 31st, 2016
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5.  
  6. /// <summary>
  7. /// Пара индексов для двумерного Int32 массива.
  8. /// </summary>
  9. public class XY
  10. {
  11.     public XY(int x, int y, int value)
  12.     {
  13.         this.X = x;
  14.         this.Y = y;
  15.         this.Value = value;
  16.     }
  17.  
  18.     public readonly int X, Y, Value;
  19.     public XY next;
  20.     public bool isEndOfThePath = false;
  21.  
  22.     public List<XY> GetNeighbours(XY[,] map)
  23.     {
  24.         List<XY> result = new List<XY>(2);
  25.  
  26.         if (X > 0)
  27.             result.Add(map[X - 1, Y]);
  28.         if (X < map.GetLength(0) - 1)
  29.             result.Add(map[X + 1, Y]);
  30.  
  31.         if (Y > 0)
  32.             result.Add(map[X, Y - 1]);
  33.         if (Y < map.GetLength(1) - 1)
  34.             result.Add(map[X, Y + 1]);
  35.  
  36.         return result;
  37.     }
  38.     public override string ToString()
  39.     {
  40.         return X.ToString() + ", " + Y.ToString() + ": " + Value.ToString();
  41.     }
  42. }
  43.  
  44. public class Program
  45. {
  46.     public static void Main(String[] args)
  47.     {
  48.         int size1 = 4000, size2 = 4000;
  49.         int[,] arr = new int[size1,size2];
  50.  
  51.         Random random = new Random();
  52.         for (int x = 0; x < size1; x++)
  53.             for (int y = 0; y < size2; y++)
  54.                 arr[x, y] = random.Next();
  55.  
  56.         var timer = new Stopwatch();
  57.         timer.Start();
  58.         Console.WriteLine("Длина пути: " + GetLongestPathInArray(arr).Count);
  59.         timer.Stop();
  60.         Console.WriteLine("Память: " + (Process.GetCurrentProcess().WorkingSet64 / (1024 * 1024)).ToString() + " MB");
  61.         Console.WriteLine("Временя: " + timer.ElapsedMilliseconds);
  62.  
  63.         //GetLongestPathInArray(arr).ForEach(x => Console.WriteLine(x.GetValue(arr)));
  64.     }
  65.  
  66.     public static List<XY> GetLongestPathInArray(int[,] arr)
  67.     {
  68.         XY[,] map = new XY[arr.GetLength(0), arr.GetLength(1)];
  69.  
  70.         for (int x = 0; x < map.GetLength(0); x++)
  71.             for (int y = 0; y < map.GetLength(1); y++)
  72.                 map[x, y] = new XY(x, y, arr[x, y]);
  73.  
  74.         int length = -1;
  75.         XY bestElement = null;
  76.         foreach (XY element in map)
  77.         {
  78.             int tmpLength = GetPathLength(map, map[element.X, element.Y]);
  79.             if (tmpLength > length)
  80.             {
  81.                 bestElement = element;
  82.                 length = tmpLength;
  83.             }
  84.         }
  85.  
  86.         return GetPathFrom(map, bestElement);
  87.     }
  88.  
  89.     private static XY GetNext(XY[,] map, XY element)
  90.     {
  91.         if (element.next == null && !element.isEndOfThePath)
  92.         {
  93.             List<XY> neighbours = element.GetNeighbours(map).FindAll(n => n.Value > element.Value);
  94.  
  95.             if (neighbours.Count == 0)
  96.                 element.isEndOfThePath = true;
  97.             else
  98.             {
  99.                 int maxLength = -1;
  100.                 XY resultNeighbour = null;
  101.                 foreach (XY neighbour in neighbours)
  102.                 {
  103.                     int length = GetPathLength(map, neighbour);
  104.                     if (length > maxLength)
  105.                     {
  106.                         resultNeighbour = neighbour;
  107.                         maxLength = length;
  108.                     }
  109.                 }
  110.  
  111.                 element.next = resultNeighbour;
  112.             }
  113.         }
  114.  
  115.         return element.next;
  116.     }
  117.  
  118.     private static int GetPathLength(XY[,] map, XY from)
  119.     {
  120.         int length = 0;
  121.         XY current = from;
  122.         while (true)
  123.         {
  124.             XY next = GetNext(map, current);
  125.  
  126.             if (next == null)
  127.                 break;
  128.             else
  129.             {
  130.                 length++;
  131.                 current = next;
  132.             }
  133.         }
  134.  
  135.         return length;
  136.     }
  137.  
  138.     private static List<XY> GetPathFrom(XY[,] map, XY from)
  139.     {
  140.         List<XY> path = new List<XY>();
  141.         path.Add(from);
  142.  
  143.         XY current = from;
  144.         while (true)
  145.         {
  146.             XY next = GetNext(map, current);
  147.             if (next == null)
  148.                 break;
  149.             else
  150.             {
  151.                 path.Add(next);
  152.                 current = next;
  153.             }
  154.         }
  155.  
  156.         return path;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement