Advertisement
dim4o

ShortestPathInMatrix

Nov 21st, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class ShortestPathInMatrix
  6. {    
  7.     static void Main()
  8.     {
  9.         int rows = int.Parse(Console.ReadLine());
  10.         int cols = int.Parse(Console.ReadLine());
  11.         int[,] matrix = new int[rows, cols];
  12.         for (int i = 0; i < rows; i++)
  13.         {
  14.             var values = Console.ReadLine().Split().Select(int.Parse).ToArray();
  15.             for (int j = 0; j < values.Length; j++)
  16.             {
  17.                 matrix[i, j] = values[j];
  18.             }
  19.         }
  20.         var graph = BuildGraphAdjacencyMatrix(matrix);
  21.         var nodesPath = DijkstraAlgorithm(graph, 0, matrix.GetLength(0) * matrix.GetLength(1) - 1);
  22.  
  23.         var result = (from n in nodesPath
  24.                       let row = n / matrix.GetLength(1)
  25.                       let col = n % matrix.GetLength(1)
  26.                       select matrix[row, col]).ToList();
  27.         Console.WriteLine("Length: {0}", result.Sum());
  28.         Console.WriteLine("Path: {0}", String.Join(" ", result));
  29.     }
  30.  
  31.     private static int[,] BuildGraphAdjacencyMatrix(int[,] matrix)
  32.     {
  33.         int rows = matrix.GetLength(0);
  34.         int cols = matrix.GetLength(1);
  35.         int[] directions = { 0, -1, -1, 0, 0, 1, 1, 0 };
  36.         var graph = new int[rows * cols, rows * cols];
  37.         for (int row = 0; row < matrix.GetLength(0); row++)
  38.         {
  39.             for (int col = 0; col < matrix.GetLength(1); col++)
  40.             {
  41.                
  42.                 for (int k = 0; k < directions.Length; k +=2)
  43.                 {
  44.                     int childRow = row + directions[k];
  45.                     int childCol = col + directions[k + 1];
  46.  
  47.                     if (childRow >=0 && childRow < rows && childCol >= 0 && childCol < cols)
  48.                     {
  49.                         var parentNode = row * matrix.GetLength(1) + col;
  50.                         var childNode = childRow * matrix.GetLength(1) + childCol;
  51.                         graph[parentNode, childNode] = matrix[childRow, childCol];
  52.                         graph[parentNode, parentNode] = 0;
  53.                     }
  54.                 }                
  55.             }
  56.         }
  57.  
  58.         return graph;
  59.     }
  60.  
  61.     private static List<int> DijkstraAlgorithm(int[,] graph, int sourceNode, int destinationNode)
  62.     {
  63.         int n = graph.GetLength(0);
  64.         int[] distance = new int[n];
  65.         for (int i = 0; i < distance.Length; i++)
  66.         {
  67.             distance[i] = int.MaxValue;
  68.         }
  69.         distance[sourceNode] = 0;
  70.  
  71.         var used = new bool[n];
  72.         var previous = new int?[n];
  73.  
  74.         while (true)
  75.         {
  76.             int minDistance = int.MaxValue;
  77.             int minNode = 0;
  78.             for (int node = 0; node < n; node++)
  79.             {
  80.                 if (!used[node] && distance[node] < minDistance)
  81.                 {
  82.                     minDistance = distance[node];
  83.                     minNode = node;
  84.                 }
  85.             }
  86.             if (minDistance == int.MaxValue)
  87.             {
  88.                 break;
  89.             }
  90.             used[minNode] = true;
  91.  
  92.             for (int i = 0; i < n; i++)
  93.             {
  94.                 if (graph[minNode, i] != 0)
  95.                 {
  96.                     int newDistance = distance[minNode] + graph[minNode, i];
  97.                     if (newDistance < distance[i])
  98.                     {
  99.                         distance[i] = newDistance;
  100.                         previous[i] = minNode;
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.  
  106.         if (distance[destinationNode] == int.MaxValue)
  107.         {
  108.             return null;
  109.         }
  110.  
  111.         var path = new Stack<int>();
  112.         int? currentNode = destinationNode;
  113.         while (currentNode != null)
  114.         {
  115.             path.Push(currentNode.Value);
  116.             currentNode = previous[currentNode.Value];
  117.         }
  118.         return path.ToList();
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement