Advertisement
g-stoyanov

SoftUniMaxPathInMatrix

Jan 20th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. namespace MaxPathInMatrix
  2. {
  3.     using System;
  4.  
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[,] matrix =
  10.             {
  11.                 { 1, 3, 7 },
  12.                 { 3, 5, 9 },
  13.                 { 6, 2, 0}
  14.             };
  15.  
  16.             int maxSum = GetMaxPathSum(matrix, 0, 0, 0, 0);
  17.             Console.WriteLine(maxSum);
  18.         }
  19.  
  20.         public static int GetMaxPathSum(int[,] matrix, int row, int col, int sum, int maxSum)
  21.         {          
  22.             if ((col + 1) < matrix.GetLength(1))
  23.             {
  24.                 maxSum = GetMaxPathSum(matrix, row, col + 1, sum + matrix[row,col], maxSum);
  25.             }
  26.  
  27.             if ((row + 1) < matrix.GetLength(0))
  28.             {
  29.                maxSum = GetMaxPathSum(matrix, row + 1, col, sum + matrix[row, col], maxSum);
  30.             }
  31.  
  32.             if ((col + 1) == matrix.GetLength(1) && (row + 1) == matrix.GetLength(0))
  33.             {
  34.                 if (sum > maxSum)
  35.                 {
  36.                     maxSum = sum;
  37.                 }
  38.             }
  39.  
  40.             return maxSum;
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement