唯一路径问题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,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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,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)

LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]的更多相关文章

  1. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  4. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  5. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. [Leetcode] unique paths ii 独特路径

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 63. Unique Paths II 动态规划

    description: https://leetcode.com/problems/unique-paths/ 机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法,这个加了难度, ...

  8. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

随机推荐

  1. tail 显示文件最后若干行内容

    功能:tail命令可以输出文件的尾部内容,默认情况下它显示文件的最后十行.显示每个指定文件的最后10 行到标准输出.若指定了多于一个文件,程序会在每段输出的开始添加相应文件名作为头.如果不指定文件或文 ...

  2. MYSQL单双向同步

    Master:192.168.1.101 Slave  :192.168.1.102 单向同步(一) 进入Master启动MYSQL [root@localhost ~]# service mysql ...

  3. 工厂方法模式与IoC/DI

    IoC——Inversion of Control  控制反转 DI——Dependency Injection   依赖注入 1:如何理解IoC/DI        要想理解上面两个概念,就必须搞清 ...

  4. java 多线程

    1.继承Thread类实现多线程继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实例,它代表一个线程的实例,并且,启动线程的唯一方法就是 ...

  5. 配置OpenCV产生flann\logger.h(66): error C4996: 'fopen': This function or variable may be unsafe问题[zz]

    使用vs2012/2013配置opencv编译出现问题: 1>------ 已启动生成: 项目: Win32ForOpenCV245, 配置: Debug Win32 ------ 1> ...

  6. input标签name与value区别

    id是唯一标识符,不允许有重复值(类似数据表的主键)可以通过它的值来获得对应的html标签对象.(如果在同一页面代码中,出现重复的id,会导致不可预料的错误) name:单独地在一个网页里面,一个控件 ...

  7. 第K大数

    控制数据箱(box.c/cpp/pas)[题目大意]现在给你一个数据箱,支持以下操作,加入元素,第 n次查询操作求当前情况下的第 n 大数.比如说,第 3 次查询操作求第三小的数.当然查询操作的给出方 ...

  8. 关于LDA的几何表示——MATLAB实现

    承接这个PCA的练习,还有一个关于LDA的几何表示. 题目如下: 代码实现LDA如下:LDA.m clear clc % 生成training sample MU1 = [6 10]'; MU2 = ...

  9. IE6,IE7文档模式下 按钮type=submit在页面打开时会有一条黑线边框的处理方法。(转)

    一:按钮border:none:同时使用背景图片来实现border效果. 二:在按钮外面嵌套一层label标签,里面的按钮input[type="submit"]的border:n ...

  10. 设置时间&时区

    设置时间之前要先了解一件事,时间分为系统时间与硬件时间 如果硬件时间与系统时间不相同的话,经常会发现自己写的程序时间可能对不上 首先修改硬件时间 1)修改时区 输入命令: tzselect 按照指示选 ...