中山大学

中山大学算法课程题目详解(第十三周)

假装没事ソ 提交于 2019-12-03 11:23:55
问题描述: 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 1: [[1,3,1], [1,5,1], [4,2,1]] Given the above grid map, return 7 . Because the path 1→3→1→1→1 minimizes the sum. 解决思路: 当然,采用动态规划的方法,从左上到右下开始进行递推。 首先,对于边界做以下处理: (1)result[i][0] = result[i - 1][0] + grid[i][0] (2)result[0][i] = result[0][i - 1] + grid[0][i] 接着,对于其他采用 result[i][j] = min(result[i - 1][j], result[i][j - 1]) + grid[i][j] 具体代码实现: class Solution { public: