首页 > 代码库 > leetCode —— Unique Paths II [Dynamic Programming]

leetCode —— Unique Paths II [Dynamic Programming]

唯一路径问题II

Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[  [0,0,0],  [0,1,0],  [0,0,0]]

The total number of unique paths is 2.

Note: m and n will be at most 100.

 --

第一种方法(uniquePathsWithObstacles)为递归实现,但是会超时,最后一个case有16亿+条路径...递归方法会走每条路径,所以一定会超时。

 

第二种方法(uniquePathsWithObstaclesDP)为动态规划。

 
‘‘‘Created on Nov 25, 2014@author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>‘‘‘class Solution:    def __init__(self):        self.ways=0        self.max_x=0        self.max_y=0            # @param obstacleGrid, a list of lists of integers    # @return an integer    def uniquePathsWithObstacles(self, obstacleGrid):        if(obstacleGrid==None):return 0        if(len(obstacleGrid)==0):return 0        if(obstacleGrid[0][0] ==1): return 0                self.__init__()        self.max_x=len(obstacleGrid[0])-1        self.max_y=len(obstacleGrid)-1        self.find_ways(0,0, obstacleGrid)        return self.ways            def find_ways(self, x, y, grid):        if(x==self.max_x and y==self.max_y):            self.ways=self.ways+1                    if(x<self.max_x and grid[y][x+1]!=1):            self.find_ways(x+1, y, grid)        if(y<self.max_y and grid[y+1][x]!=1):            self.find_ways(x, y+1, grid)                            def uniquePathsWithObstaclesDP(self, obstacleGrid):        m = len(obstacleGrid)          if(m ==0): return 0          n = len(obstacleGrid[0])          if(obstacleGrid[0][0] ==1): return 0                maxV={}        for x in range(n):maxV[x]=0                maxV[0]=1;          for i in range(m):            for j in range(n):                  if(obstacleGrid[i][j] ==1):                    maxV[j]=0                else:                     if(j >0):                          maxV[j] = maxV[j-1]+maxV[j]        return maxV[n-1];                 if __name__ == __main__:    sl=Solution()    grid=[[0,0,0],            [0,1,0],            [0,0,0]]    print sl.uniquePathsWithObstacles(grid)    grid=[[0,0,0,0,0],            [0,1,0,0,0],          [0,1,0,0,0],          [0,1,0,0,0],            [0,0,0,0,0]]    print sl.uniquePathsWithObstacles(grid)    grid= [               [0,0,0,0,0,0,0,0,0,0],               [0,0,0,0,0,1,0,0,0,0],               [0,0,0,1,0,0,0,0,0,0],               [0,0,0,0,0,0,0,0,0,0],               [0,1,0,1,0,0,0,0,0,0],               [0,0,0,0,0,0,0,0,1,0],               [0,1,0,1,0,0,0,0,0,0]            ]        print sl.uniquePathsWithObstacles(grid)    grid=[[0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],[0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0],[1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,1],[0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0],[0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,0],[1,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0],[0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0],[0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],[1,0,1,0,0,0,0,0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