Unique Paths II
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below). Now consider if some obstacles are added to the grids. How many unique paths would there be?
(不同的路径II,只能向右或者向下移动在有路障的情况下从左上角移动到右下角的路径个数)
Example:
1. 动态规划
写出其动规表达式 dp[i][j] = dp[i-1][j] + dp[i][j-1]。但是需要另外考虑:
- 遇到路障时,dp[i][j] = 0,继续扫描。
- 由于从左上角开始扫描,在扫描第一行时:dp[i][j] = dp[i][j-1]。
- 在扫描第一列时:dp[i][j] = dp[i-1][j] 。
具体实现形式如下:
1 | class Solution: |