zoukankan      html  css  js  c++  java
  • 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]}

    
    
    苟有恒,何必三更眠五更起;最无益,莫过一日暴十日寒。
  • 相关阅读:
    POJ 1044: Date bugs
    POJ 1017: Packets
    POJ 1014: Dividing
    POJ 1012: Joseph
    POJ 1011: Sticks
    POJ 1008: Maya Calendar
    POJ 1005: I Think I Need a Houseboat
    为什么要自动化测试
    微软CodeDom模型学习笔记(全)
    概念完整性
  • 原文地址:https://www.cnblogs.com/shaer/p/10519780.html
Copyright © 2011-2022 走看看