dfs 深度搜索超时
/*
dp[i][j] 从0,0 到 i,j 的路径最短距离
dp[i][j] = matrix[i][j] + min(dp[i-1][j],dp[i][j-1] )
*/

class Solution {
public:
    int minPathSum(vector<vector<int> >& matrix) {
        int n = matrix.size();
        int m = matrix[0].size();
        vector<vector<int> > dp(n,vector<int>(m));
        dp[0][0] = matrix[0][0];
        for(int i = 1; i < n; i++) dp[i][0] = dp[i-1][0] + matrix[i][0];
        for(int i = 1; i < m; i++) dp[0][i] = dp[0][i-1] + matrix[0][i];
        for(int i = 1; i < n; i++){
            for(int j = 1; j < m; j++){
                dp[i][j] = matrix[i][j] + min(dp[i-1][j],dp[i][j-1]);
            }
        }
        return dp[n-1][m-1];
    }
};
// 可以降低空间复杂度 去掉dp 
class Solution {
public:
    int minPathSum(vector<vector<int> >& matrix) {
        int n = matrix.size();
        int m = matrix[0].size();
        for(int i = 1 ; i < n ; i++) matrix[i][0] = matrix[i-1][0]+ matrix[i][0];
        for(int j = 1 ; j < m ; j++) matrix[0][j] = matrix[0][j-1]+  matrix[0][j];
        for(int i = 1 ; i < n ; i++)
        {
            for(int j = 1 ; j < m ; j++)
            {
              matrix[i][j] = min(matrix[i-1][j],matrix[i][j-1])+matrix[i][j];
            }
        }
       return matrix[n-1][m-1];
    }
};