Advertisement
ArXen42

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

Mar 28th, 2016
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public struct XY
  6. {
  7.     public XY(int x, int y)
  8.     {
  9.         this.x = x;
  10.         this.y = y;
  11.     }
  12.  
  13.     public int x, y;
  14.  
  15.     public int GetValue(int[,] arr)
  16.     {
  17.         return arr[x, y];
  18.     }
  19.  
  20.     public override string ToString()
  21.     {
  22.         return x.ToString() + ", " + y.ToString();
  23.     }
  24. }
  25.  
  26. public class Program
  27. {
  28.     public static void Main(String[] args)
  29.     {
  30.         int[,] arr = new int[,]
  31.         {
  32.             {2,5,1,0},
  33.             {3,3,1,9},
  34.             {4,4,7,8}
  35.         };
  36.  
  37.  
  38.         GetLongestPath(arr).ForEach(x => Console.WriteLine(x.GetValue(arr)));
  39.     }
  40.  
  41.     public static List<XY> GetLongestPath(int[,] arr)
  42.     {
  43.  
  44.         List<XY> longestPath = new List<XY>(), currentLongestPath;
  45.  
  46.         for (int x = 0; x < arr.GetLength(0); x++)
  47.             for (int y = 0; y < arr.GetLength(1);y++)
  48.             {
  49.                 currentLongestPath = new List<XY>(){new XY(x, y)};
  50.                 CalculateLongestPathFrom(arr, currentLongestPath);
  51.                 if (currentLongestPath.Count > longestPath.Count)
  52.                     longestPath = new List<XY>(currentLongestPath);
  53.             }
  54.  
  55.         longestPath = new List<XY>(){new XY(1, 2)};
  56.         CalculateLongestPathFrom(arr, longestPath);
  57.  
  58.         Console.WriteLine();
  59.  
  60.         return longestPath;
  61.     }
  62.  
  63.     private static void CalculateLongestPathFrom(int[,] arr, List<XY> path)
  64.     {
  65.         XY last = path.Last();
  66.  
  67.         List<XY> neighbours = GetNeighbours(arr, last).FindAll(n => n.GetValue(arr) > last.GetValue(arr));
  68.  
  69.         if (neighbours.Count > 0)
  70.         {
  71.             List<XY> longestPath = new List<XY>(0);
  72.             foreach (XY neighbour in neighbours)
  73.             {
  74.                 List<XY> tmpPath = new List<XY>(path);
  75.                 tmpPath.Add(neighbour);
  76.  
  77.                 CalculateLongestPathFrom(arr, tmpPath);
  78.                 if (tmpPath.Count > longestPath.Count)
  79.                     longestPath = new List<XY>(tmpPath);
  80.             }
  81.  
  82.             path.Clear();
  83.             foreach (var el in longestPath)
  84.                 path.Add(el);
  85.         }
  86.     }
  87.  
  88.     private static List<XY> GetNeighbours(int[,] arr, XY indexes)
  89.     {
  90.         List<XY> result = new List<XY>(2);
  91.  
  92.         if (indexes.x > 0)
  93.             result.Add(new XY(indexes.x - 1, indexes.y));
  94.         if (indexes.x < arr.GetLength(0) - 1)
  95.             result.Add(new XY(indexes.x + 1, indexes.y));
  96.  
  97.         if (indexes.y > 0)
  98.             result.Add(new XY(indexes.x, indexes.y - 1));
  99.         if (indexes.y < arr.GetLength(1) - 1)
  100.             result.Add(new XY(indexes.x, indexes.y + 1));
  101.  
  102.         return result;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement