64. Minimum Path Sum(最短路径)

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.

Example:

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
题目描述:求从矩阵的左上角到右下角的最小路径和,每次只能向右和向下移动。
分析:从前往后看是每个点只有两个选择,要不然向右要不然向左。同理从后往前看也只有两种选择要不然向上要不然向左。
我之前想到这个思路了,可是还是不知道如何下手。最后又一看题,题目要求写最短路径的值并没要求写最短路径的数组。
那么问题就好做多了,不用开辟新数组存值了。
除了第一行第一列特殊外,其他的都满足 dp[i][j] =min{dp[i][j-1],dp[i-1][j]}

 


posted on 2019-03-12 21:27  shaer  阅读(119)  评论(0编辑  收藏  举报

导航