let's say I have a matrix n ^ n . There is a positive number in each cell, and I need to get the path with the smallest weight .
a path is every path starting in matrix[0][0] and ending in matrix[matrix.length-1][matrix[0].length-1] , without diagonals.
path weight is the number of all numbers in it.
For example, suppose I have this matrix:
mat = {{1,2},{3,4}};
therefore, {1,2,4} is the path , and {1,3,4} is another path .
The weight of the first path is 7 (1 + 2 + 4), and the second has a weight of 8 (1 + 3 + 4), so the function will return 7 .
I need to solve it recursively. The pseudocode should look something like this:
if i > matrix.length-1 || i < 0 || j > matrix[0].length-1 || j < 0
Then call four recursive calls:
public static int smallPath(a, i+1, j, sum); public static int smallPath(a, i, j+1, sum); public static int smallPath(a, i-1, j, sum); public static int smallPath(a, i, j-1, sum);
Then I need to get something, what?
I know this is not relevant, but what is the general idea, can someone help me solve this? Thanks!