class Solution { public: /** * * @param matrix int整型vector<vector<>> the matrix * @return int整型 */ int minPathSum(vector<vector<int> >& matrix) { int n = matrix.size(); int m = matrix[0].size(); vector<vector<int> > dp(n + 1, vector<int>(m + 1, 0)); for (int i = 1; i < n + 1; i++) { dp[i][1] = matrix[i - 1][0] + dp[i - 1][1]; } for (int j = 1; j < m + 1; j++) { dp[1][j] = matrix[0][j - 1] + dp[1][j - 1]; }// 初始化值 for (int i = 2; i < n + 1; i++) { for (int j = 2; j < m + 1; j++) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + matrix[i - 1][j - 1]; } } return dp[n][m]; } };