class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int distance = 0; //用来计算距离
if(grid.size() == 0) //判断,如果gird无东西,直接返回0
return distance;
int length = grid[0].size(); //获取长
int wide = grid.size(); //获取高
//在原先的gird数组里构建dp数组
for(int i =0;i<wide;++i)
{
for(int j=0;j<length;++j)
{
if(i == 0 && j == 0) continue; //在左上角的时候直接跳过
if(i == 0) {grid[i][j]+=grid[i][j-1]; continue;}
if(j == 0) {grid[i][j]+=grid[i-1][j];continue;}
if(i!=0 && j!=0){grid[i][j]+= min(grid[i-1][j],grid[i][j-1]);} //因为题目说的走的路线都是下/右,所以找最小加当前值即可
}
}
return grid[wide-1][length-1]; //返回最短距离
}
};