题目描述
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 109.
翻译:一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
问总共有多少条不同的路径?
示例:
输入:m = 3, n = 7 输出:28
动态规划解法
解题思路与步骤
为了方便表示,我们将这个图命名为dp,机器人的起始位置为Start,目标位置为Finish,而dp中任意一个网格的下标为[i][j],表示这个网格时称为dp(i,j),表示这个网格的值时称为dp[i][j].
理解题意
首先,我们应该明确在这道题中每一个网格dp(i,j)的含义为什么,我们以这张图为例:
而去往dp(i,j)的路径只有一条:
这是可能有杠精要说了:啊啊,你是不是傻,我不可以先往下走一格再绕过去吗,这样路径不就多多了吗:
拜托,读读题吧,题目已经说了,机器人每次只能向下或者向右移动一步,所以说此时机器人走到dp(i,j)的路径只有一条,也就是说此时的dp[i][j]=1。
那么dp中的每一个网格的含义就已经很明确了,为:
d p [ i ] [ j ] = F i n i s h 到 d p ( i , j ) 总共的路径或是走法 dp[i][j]=Finish到dp(i,j)总共的路径或是走法 dp[i][j]