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

最新文章

  1. React之Froms
  2. 自定义ViewGroup
  3. Windows通过DOS命令进入MYSQL的方法
  4. angularjs源码分析之:angularjs执行流程
  5. 【原创】ORA-04068: 已丢弃程序包 的当前状态研究
  6. js 一个关于图片onload加载的事
  7. python之6-2高阶函数
  8. Linux系统中如何添加自己的库文件路径
  9. 【百度地图API】如何调整结果面板的样式?如何获取指定页码的结果?
  10. python xml sendEmail
  11. PHP正则表达式二分法实现mysql盲注脚本
  12. Effective java 系列之更优雅的关闭资源-try-with-resources
  13. H5判断手机是否存在应用和打开应用
  14. MYSQL使用中字符编码一坑
  15. keepalived基础原理
  16. 背水一战 Windows 10 (43) - C# 7.0 新特性
  17. SQL 数据库事务 存储过程练习
  18. 解决Navicat连接Oracle时报错ORA-28547
  19. python StringIO类
  20. linux下使用supervisor启动.net core mvc website的配置

热门文章

  1. h5py
  2. 为什么会有object这么一个根基类
  3. 使用rosed编辑ROS中的文件
  4. 50道Java线程面试题(转载)
  5. 第一章:AI人工智能 の 数据预处理编程实战 Numpy, Pandas, Matplotlib, Scikit-Learn
  6. Eclipse设置格式化每行字符的长度
  7. 数据库分库分表和带来的唯一ID、分页查询问题的解决
  8. Mysql5.7.21 Navicat触发器创建
  9. Vue.js下载方式及基本概念
  10. Linux学习笔记(第十章)