动态规划_leetcode63
Posted AceKo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划_leetcode63相关的知识,希望对你有一定的参考价值。
#coding=utf-8
# 递归
# dfs
class Solution1(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
if not obstacleGrid:
return
self.m = len(obstacleGrid)
self.n = len(obstacleGrid[0])
self.direction = [[0,1],[1,0]]
self.visit = [[False for i in range(self.n)] for i in range(self.m)]
self.res = 0
if obstacleGrid[0][0] == 1:
return self.res
self.findPath(obstacleGrid,0,0)
print self.res
return self.res
def inArea(self,x,y):
return x >= 0 and x < self.m and y >=0 and y < self.n
def findPath(self,obstacleGrid,x,y):
if x == self.m-1 and y == self.n-1:
self.res += 1
self.visit[x][y] = True
for item in self.direction:
newX = x + item[0]
newY = y + item[1]
if self.inArea(newX,newY) and not self.visit[newX][newY] and obstacleGrid[newX][newY] != 1:
self.findPath(obstacleGrid,newX,newY)
self.visit[x][y] = False
# s = Solution1()
#
# ob = [
# [0,0,0],
# [0,1,0],
# [0,0,0]
# ]
#
#
# s.uniquePathsWithObstacles(ob)
# 记忆化递归
class Solution2(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
if not obstacleGrid:
return
self.m = len(obstacleGrid)
self.n = len(obstacleGrid[0])
if obstacleGrid[0][0] == 1:
return 0
self.memo = [[-1 for i in range(self.n+1)] for i in range(self.m+1)]
for i in range(self.n+1):
self.memo[0][i] = 0
for i in range(self.m+1):
self.memo[i][0] = 0
self.memo[1][1] = 1
self.findPath(obstacleGrid,self.m,self.n)
print self.memo[self.m][self.n] return self.memo[self.m][self.n] def findPath(self,obstacleGrid,x,y): if self.memo[x][y] != -1: return self.memo[x][y] else: #? # 原矩阵 与状态 举证的对应关系 if obstacleGrid[x-1][y-1] == 1: #self.memo[x][y] 为障碍物位置 self.memo[x][y] = 0 return self.memo[x][y] if self.memo[x-1][y] != -1: upValue = self.memo[x-1][y] else: self.memo[x-1][y] = self.findPath(obstacleGrid,x-1,y) upValue = self.memo[x - 1][y] if self.memo[x][y-1] != -1: leftValue = self.memo[x][y-1] else: self.memo[x][y-1] = self.findPath(obstacleGrid,x,y-1) leftValue = self.memo[x][y-1] self.memo[x][y] = upValue + leftValue return self.memo[x][y]# s = Solution2()## ob = [# [0,0,0],# [0,1,0],# [0,0,0]# ]### s.uniquePathsWithObstacles(ob)# 动态规划class Solution3(object): def uniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[List[int]] :rtype: int """ if not obstacleGrid: return self.findPath(obstacleGrid) return self.findPath(obstacleGrid) def findPath(self,obstacleGrid): m = len(obstacleGrid) n = len(obstacleGrid[0]) if obstacleGrid[0][0] == 1: return 0 memo = [[-1 for i in range(n+1)] for i in range(m+1)] for i in range(n + 1): memo[0][i] = 0 for i in range(m + 1): memo[i][0] = 0 memo[1][1] = 1 for i in range(1,m+1): for j in range(1,n+1): if i == 1 and j == 1: continue #self.memo[x][y] 为障碍物位置 if obstacleGrid[i-1][j-1] == 1: memo[i][j] = 0 continue memo[i][j] = memo[i-1][j] + memo[i][j-1] return memo[m][n]s = Solution3()ob = [ [0,0,0], [0,1,0], [0,0,0]]print s.uniquePathsWithObstacles(ob)
以上是关于动态规划_leetcode63的主要内容,如果未能解决你的问题,请参考以下文章
算法动态规划 ⑤ ( LeetCode 63.不同路径 II | 问题分析 | 动态规划算法设计 | 代码示例 )
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)