思路:
dp[i][j]含义:走到第(i,j)个格子有多少种方法。
递推公式:
初始化:(一开始整个dp数组都初始化为0)
针对第一行第一列:
遍历顺序:从上到下,从左到右
- class Solution(object):
- def uniquePathsWithObstacles(self, obstacleGrid):
- n=len(obstacleGrid)
- m=len(obstacleGrid[0])
- if obstacleGrid[0][0]==1 or obstacleGrid[n-1][m-1]==1:return 0
- dp=[[0 for _ in range(m)] for _ in range(n)]
- for i in range(n):
- if obstacleGrid[i][0]==0:
- dp[i][0]=1
- else:
- break
- for j in range(m):
- if obstacleGrid[0][j]==0:
- dp[0][j]=1
- else:
- break
- for i in range(1,n):
- for j in range(1,m):
- if obstacleGrid[i][j]==1:
- continue
- else:
- dp[i][j]=dp[i-1][j]+dp[i][j-1]
- return dp[n-1][m-1]