zoukankan      html  css  js  c++  java
  • 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)为动态规划

      不难发现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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0,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,1],
    [0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1],
    [1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,1,1,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,1,1],
    [0,0,0,1,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,1,0,0,0]
    ]
        print sl.uniquePathsWithObstaclesDP(grid)
  • 相关阅读:
    easyui的dataGrid生成的日期时间,总是不能很好的兼容ie8和谷歌,终于摸索出一个合适的办法
    DELPHI使用TClientDataSet时不携带MIDAS.DLL的方法
    你又重新年轻了一次,这一次你打算怎么活?
    c#网站项目的发布:项目方式、webSite网站模式(未能获得项目引用XXX的依赖项的解决)
    当取不到raisError的错误信息只能取到return的错误代码时,可以取connection.errors[0].description
    layer iframe大致使用
    全选
    下拉选
    checkbox
    js判断值对否为空
  • 原文地址:https://www.cnblogs.com/scottgu/p/4121628.html
Copyright © 2011-2022 走看看