Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MaxPathInMatrix
- {
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- int[,] matrix =
- {
- { 1, 3, 7 },
- { 3, 5, 9 },
- { 6, 2, 0}
- };
- int maxSum = GetMaxPathSum(matrix, 0, 0, 0, 0);
- Console.WriteLine(maxSum);
- }
- public static int GetMaxPathSum(int[,] matrix, int row, int col, int sum, int maxSum)
- {
- if ((col + 1) < matrix.GetLength(1))
- {
- maxSum = GetMaxPathSum(matrix, row, col + 1, sum + matrix[row,col], maxSum);
- }
- if ((row + 1) < matrix.GetLength(0))
- {
- maxSum = GetMaxPathSum(matrix, row + 1, col, sum + matrix[row, col], maxSum);
- }
- if ((col + 1) == matrix.GetLength(1) && (row + 1) == matrix.GetLength(0))
- {
- if (sum > maxSum)
- {
- maxSum = sum;
- }
- }
- return maxSum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement