Advertisement
ArXen42

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

Apr 5th, 2016
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5.  
  6. public class Program
  7. {
  8.     struct Point
  9.     {
  10.         public Point(int x, int y)
  11.         {
  12.             this.X = x;
  13.             this.Y = y;
  14.         }
  15.         public readonly int X,Y;
  16.  
  17.         public int GetValue(int[,] arr)
  18.         {
  19.             return arr[X, Y];
  20.         }
  21.     }
  22.  
  23.     public static void Main(String[] args)
  24.     {
  25.         //arr = new int[,] {{2,5,1,0}, {3,3,1,9}, {4,4,7,8}};
  26.  
  27.         int size1 = 4000, size2 = 4000;
  28.         arr = new int[size1,size2];
  29.  
  30.         Random random = new Random();
  31.         for (int x = 0; x < size1; x++)
  32.             for (int y = 0; y < size2; y++)
  33.                 arr[x, y] = random.Next();
  34.  
  35.         var timer = new Stopwatch();
  36.         timer.Start();
  37.  
  38.         map = InitializeMap();
  39.  
  40.         var path = new List<Point>();
  41.         BacktracePath(path, longestPathEnd.X, longestPathEnd.Y);
  42.         path.Reverse();
  43.  
  44.         timer.Stop();
  45.  
  46.         Console.WriteLine("Длина пути: {0}", path.Count);
  47.         Console.WriteLine("Память: {0} MiB", Process.GetCurrentProcess().WorkingSet64 / (1024 * 1024));
  48.         Console.WriteLine("Временя: {0} ms", timer.ElapsedMilliseconds);
  49.  
  50.         //path.ForEach(x => Console.WriteLine(arr[x.X, x.Y]));
  51.     }
  52.  
  53.     private static int[,] arr, map;
  54.     private static Point longestPathEnd;
  55.  
  56.     private static int[,] InitializeMap()
  57.     {
  58.         map = new int[arr.GetLength(0),arr.GetLength(1)];
  59.         for (int x = 0; x < map.GetLength(0); x++)
  60.             for (int y = 0; y < map.GetLength(1); y++)
  61.                 map[x, y] = -1;
  62.  
  63.         for (int x = 0; x < map.GetLength(0); x++)
  64.             for (int y = 0; y < map.GetLength(1); y++)
  65.                 WaveExpansion(x, y);
  66.  
  67.         return map;
  68.     }
  69.  
  70.     private static void WaveExpansion(int x, int y, int range = 0)
  71.     {
  72.         if (map[x, y] < range)
  73.         {
  74.             map[x, y] = range;
  75.  
  76.             if (range > longestPathEnd.GetValue(map))
  77.                 longestPathEnd = new Point(x, y);
  78.  
  79.             int thisCellValue = arr[x, y];
  80.  
  81.             if (x > 0)
  82.             if (arr[x - 1, y] > thisCellValue)
  83.                 WaveExpansion(x - 1, y, range + 1);
  84.  
  85.  
  86.             if (x < map.GetLength(0) - 1)
  87.             if (arr[x + 1, y] > thisCellValue)
  88.                 WaveExpansion(x + 1, y, range + 1);
  89.  
  90.  
  91.             if (y > 0)
  92.             if (arr[x, y - 1] > thisCellValue)
  93.                 WaveExpansion(x, y - 1, range + 1);
  94.  
  95.             if (y < map.GetLength(1) - 1)
  96.             if (arr[x, y + 1] > thisCellValue)
  97.                 WaveExpansion(x, y + 1, range + 1);
  98.         }
  99.     }
  100.  
  101.     private static void BacktracePath(List<Point> path, int x, int y)
  102.     {
  103.         path.Add(new Point(x, y));
  104.         int soughtCellValue = map[x, y] - 1;
  105.  
  106.         if (x > 0)
  107.         if (map[x - 1, y] == soughtCellValue)
  108.             BacktracePath(path, x - 1, y);
  109.  
  110.  
  111.         if (x < map.GetLength(0) - 1)
  112.         if (map[x + 1, y] == soughtCellValue)
  113.             BacktracePath(path, x + 1, y);
  114.  
  115.  
  116.         if (y > 0)
  117.         if (map[x, y - 1] == soughtCellValue)
  118.             BacktracePath(path, x, y - 1);
  119.  
  120.         if (y < map.GetLength(1) - 1)
  121.         if (map[x, y + 1] == soughtCellValue)
  122.             BacktracePath(path, x, y + 1);
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement