Minimum path and matrix path of the matrix
Classic dynamic programming method.
If the given matrix is as follows:
1 3 5 9
8 1 3 4
5 0 6 1
8 8 4 0
Paths 1, 3, 1, 0, 6, 1, and 0 are the smallest paths and values in all paths. 1 + 3 + 1 + 0 + 6 + 1 + 0 = 12, so 12 is returned.
The Code is as follows:
Public class demo5 {
Public static void main (String [] args ){
Int [] [] array = }};
Int [] [] result = minPathSum (array );
System. out. println (result [result. length-1] [result [0]. length-1]);
}
Public static int [] [] minPathSum (int [] [] array ){
Int row = array. length;
Int col = array [0]. length;
Int [] [] m = new int [row] [col];
M [0] [0] = array [0] [0];
For (int I = 1; I <row; ++ I ){
M [I] [0] = array [I] [0] + m [I-1] [0];
}
For (int j = 1; j <col; ++ j ){
M [0] [j] = array [0] [j] + m [0] [J-1];
}
For (int k = 1; k <row; ++ k ){
For (int kk = 1; kk <col; ++ kk ){
M [k] [kk] = Math. min (m [k-1] [kk], m [k] [kk-1]) + array [k] [kk];
}
}
Return m;
}
}