Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
注意关系对应之处
class Solution { public: int minPathSum(vector<vector<int> > &grid) { int m,n; int sum = 0; m = grid.size(); n = grid[0].size(); if(m*n==0)return 0; int dp[m+1][n+1]; dp[1][1] = grid[0][0]; int i,j; for(i=2;i<m+1;i++) { dp[i][1] = dp[i-1][1] + grid[i-1][0]; } for(i=2;i<n+1;i++) { dp[1][i] = dp[1][i-1] + grid[0][i-1]; } for(i=2;i<=m;i++) for(j=2;j<=n;j++) { if(dp[i-1][j] < dp[i][j-1]) dp[i][j] = dp[i-1][j]+grid[i-1][j-1]; else dp[i][j] = dp[i][j-1]+grid[i-1][j-1]; } return dp[m][n]; } };