Advertisement
ArXen42

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

Apr 4th, 2016
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 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 override string ToString()
  23.     {
  24.         return X.ToString() + ", " + Y.ToString() + ": " + Value.ToString();
  25.     }
  26. }
  27.  
  28. public class Program
  29. {
  30.     public static void Main(String[] args)
  31.     {
  32.         int size1 = 4000, size2 = 4000;
  33.         arr = new int[size1,size2];
  34.  
  35.         Random random = new Random();
  36.         for (int x = 0; x < size1; x++)
  37.             for (int y = 0; y < size2; y++)
  38.                 arr[x, y] = random.Next();
  39.  
  40.         var timer = new Stopwatch();
  41.         timer.Start();
  42.         Console.WriteLine("Длина пути: {0}", GetLongestPathInArray().Count);
  43.         timer.Stop();
  44.         Console.WriteLine("Память: {0} MiB", Process.GetCurrentProcess().WorkingSet64 / (1024 * 1024));
  45.         Console.WriteLine("Временя: {0} ms", timer.ElapsedMilliseconds);
  46.     }
  47.  
  48.     public static List<XY> GetLongestPathInArray()
  49.     {
  50.         map = new XY[arr.GetLength(0), arr.GetLength(1)];
  51.  
  52.         for (int x = 0; x < map.GetLength(0); x++)
  53.             for (int y = 0; y < map.GetLength(1); y++)
  54.                 map[x, y] = new XY(x, y, arr[x, y]);
  55.  
  56.         foreach (XY element in map)
  57.             GetNext(element);
  58.  
  59.         return GetPathFrom(longestPathCell);
  60.     }
  61.  
  62.     private static int[,] arr;
  63.     private static XY[,] map;
  64.  
  65.     private static XY longestPathCell;
  66.     private static int longestPathLength;
  67.  
  68.     private static XY GetNext(XY element)
  69.     {
  70.         //Длинно, но работает сильно быстрее, чем создание и проход по списку соседей.
  71.         if (element.next == null && !element.isEndOfThePath)
  72.         {
  73.             XY left,right,down,up; //Соседи
  74.             left = right = up = down = null;
  75.  
  76.             if (element.X > 0)
  77.                 left = map[element.X - 1, element.Y];
  78.             if (element.X < map.GetLength(0) - 1)
  79.                 right = map[element.X + 1, element.Y];
  80.  
  81.             if (element.Y > 0)
  82.                 down = map[element.X, element.Y - 1];
  83.             if (element.Y < map.GetLength(1) - 1)
  84.                 up = map[element.X, element.Y + 1];
  85.  
  86.             int leftLength = GetNeighbourPathLength(map, element, left);
  87.             int rightLength = GetNeighbourPathLength(map, element, right);
  88.             int downLength = GetNeighbourPathLength(map, element, down);
  89.             int upLength = GetNeighbourPathLength(map, element, up);
  90.  
  91.             XY resultNeighbour = null;
  92.  
  93.             int maxLength = -1;
  94.  
  95.             if (leftLength > maxLength)
  96.             {
  97.                 maxLength = leftLength;
  98.                 resultNeighbour = left;
  99.             }
  100.             if (rightLength > maxLength)
  101.             {
  102.                 maxLength = rightLength;
  103.                 resultNeighbour = right;
  104.             }
  105.             if (downLength > maxLength)
  106.             {
  107.                 maxLength = downLength;
  108.                 resultNeighbour = down;
  109.             }
  110.             if (upLength > maxLength)
  111.             {
  112.                 maxLength = upLength;
  113.                 resultNeighbour = up;
  114.             }
  115.  
  116.  
  117.             element.next = resultNeighbour;
  118.  
  119.             if (resultNeighbour == null)
  120.                 element.isEndOfThePath = true;
  121.         }
  122.  
  123.         return element.next;
  124.     }
  125.  
  126.     private static int GetNeighbourPathLength(XY[,] map, XY parent, XY neighbour)
  127.     {
  128.         if (neighbour != null && neighbour.Value > parent.Value)
  129.         {
  130.             return GetPathLength(neighbour);
  131.         }
  132.         else
  133.             return -1;
  134.     }
  135.    
  136.     private static int GetPathLength(XY from)
  137.     {
  138.         int length = 0;
  139.         XY current = from;
  140.         while (true)
  141.         {
  142.             XY next = GetNext(current);
  143.  
  144.             if (next == null)
  145.                 break;
  146.             else
  147.             {
  148.                 length++;
  149.                 current = next;
  150.             }
  151.         }
  152.  
  153.         if (length > longestPathLength)
  154.         {
  155.             longestPathCell = from;
  156.             longestPathLength = length;
  157.         }
  158.  
  159.         return length;
  160.     }
  161.  
  162.     private static List<XY> GetPathFrom(XY from)
  163.     {
  164.         List<XY> path = new List<XY>();
  165.         path.Add(from);
  166.  
  167.         XY current = from;
  168.         while (true)
  169.         {
  170.             XY next = GetNext(current);
  171.             if (next == null)
  172.                 break;
  173.             else
  174.             {
  175.                 path.Add(next);
  176.                 current = next;
  177.             }
  178.         }
  179.         return path;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement