又一道dp
一般情况下的状态转移方程是dp[i][j] = min(dp[i - 1][j], dp[i][j - 1])+grid[i][j];
通过dp数组来计算,最后返回dp[m - 1][n - 1]
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> dp(m, vector<int>(n,0));
for (int i = 0; i<m; i++)
for (int j = 0; j<n; j++)
{
if(i-1<0&&j-1<0) dp[0][0] = grid[0][0];
else if (i - 1<0)dp[i][j] = grid[i][j] + dp[i][j - 1];
else if (j - 1<0)dp[i][j] = grid[i][j] + dp[i - 1][j];
else dp[i][j] = min(dp[i - 1][j], dp[i][j - 1])+grid[i][j];
}
return dp[m - 1][n - 1];
}