LEETCODE —— Unique Paths II [Dynamic Programming]

惟一路徑問題IIide

Unique Paths II

Follow up for "Unique Paths":spa

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

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

For example,遞歸

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

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

The total number of unique paths is 2.io

Note: m and n will be at most 100.class

 --效率

第一種方法(uniquePathsWithObstacles)爲遞歸實現grid

  會超時,最後一個case有16億+條路徑...遞歸方法會走每條路徑,因此必定會超時。

 

第二種方法(uniquePathsWithObstaclesDP)爲動態規劃

  不難發現max_ways[x,y]=max_ways[x-1,y]+max_ways[x,y-1], 即知足最優子結構性質。

  而且max_ways[x-1,y]和max_ways[x,y-1]依賴於max_ways[m,n](0<m<x, 0<n<y),即知足子問題重疊性質,所以使用動態規劃能夠得到更好的效率

 
 
'''
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)
            
    # @obstacleGrid is a grid of m*n cells     
    def uniquePathsWithObstaclesDP(self, obstacleGrid):
        m = len(obstacleGrid)  
        if(m ==0): return 0  
        n = len(obstacleGrid[0])  
        if(obstacleGrid[0][0] ==1): return 0
        
        max_ways={}
        for x in range(n):max_ways[x]=0
        
        max_ways[0]=1;  
        for y in range(m):
            for x in range(n):  
                if(obstacleGrid[y][x] ==1):
                    max_ways[x]=0
                else: 
                    if(x >0):  
                        max_ways[x] = max_ways[x-1]+max_ways[x]
        return max_ways[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,0,0,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,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,0,0,0,0,0],
               [0,0,0,0,0,0,0,0,0,0],
               [0,0,0,0,0,0,0,0,0,0],
               [0,0,0,0,0,0,0,0,0,0],
               [0,0,0,0,0,0,0,0,0,0],
               [0,0,0,0,0,0,0,0,0,0],
               [0,0,0,0,0,0,0,0,0,0] ,
               [0,0,0,0,0,0,0,0,0,0] ,
               [0,0,0,0,0,0,0,0,0,0] ,
               [0,0,0,0,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,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