从矩阵左上角走到矩阵右下角,求最短路径
import java.lang.Math; public class Dynamic_1{ static int m = 4; static int n = 4; static int sum = 0; static int[][] state = {{1, 3, 5, 9}, {2, 1, 3, 4}, {5, 2, 6, 7}, {6, 8, 4, 3}}; static int[][] matrix = new int[m][n]; public static void dynamic_1(){ for(int i = 0; i < m; i++){ sum = sum + state[i][0]; matrix[i][0] = sum; } sum = 0; for(int j = 0; j < n; j++){ sum = sum + state[0][j]; matrix[0][j] = sum; } for(int i = 1; i < m; i++){ for(int j = 1; j < n; j++){ matrix[i][j] = state[i][j] + Math.min(matrix[i - 1][j], matrix[i][j - 1]); } } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ System.out.print(matrix[i][j] + " "); } System.out.println(); } } public static void main(String[] argv){ dynamic_1(); } }