1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| public class Code_07_MinPath { public static int process1(int[][] matrix, int i, int j) { if (i == matrix.length - 1 && j == matrix[0].length - 1) { return matrix[i][j]; } if (i == matrix.length - 1) { return matrix[i][j] + process1(matrix, i, j + 1); } if (j == matrix[0].length - 1) { return matrix[i][j] + process1(matrix, i + 1, j); } int right = process1(matrix, i, j + 1); int down = process1(matrix, i + 1, j); return matrix[i][j] + Math.min(right, down); }
public static int minPath2(int[][] m) { if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) { return 0; } int row = m.length; int col = m[0].length; int[][] dp = new int[row][col]; dp[0][0] = m[0][0]; for (int i = 1; i < row; i++) { dp[i][0] = dp[i - 1][0] + m[i][0]; } for (int j = 1; j < col; j++) { dp[0][j] = dp[0][j - 1] + m[0][j]; } for (int i = 1; i < row; i++) { for (int j = 1; j < col; j++) { dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + m[i][j]; } } return dp[row - 1][col - 1]; }
public static int[][] generateRandomMatrix(int rowSize, int colSize) { if (rowSize < 0 || colSize < 0) { return null; } int[][] result = new int[rowSize][colSize]; for (int i = 0; i != result.length; i++) { for (int j = 0; j != result[0].length; j++) { result[i][j] = (int) (Math.random() * 10); } } return result; }
public static void main(String[] args) { int[][] m = {{1, 3, 5, 9}, {8, 1, 3, 4}, {5, 0, 6, 1}, {8, 8, 4, 0}}; System.out.println(process1(m, 0, 0)); System.out.println(minPath2(m));
m = generateRandomMatrix(6, 7); System.out.println(process1(m, 0, 0)); System.out.println(minPath2(m)); } }
|