• 动态规划_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)
  • 相关阅读:
    超哥笔记--linux准备知识(1)
    爬虫系列---scrapy全栈数据爬取框架(Crawlspider)
    爬虫系列---scrapy post请求、框架组件和下载中间件+boss直聘爬取
    pymongo 一篇文章搞定
    一篇文章搞定mongodb
    python进阶(四) windows下虚拟环境使用
    java基础(四) -变量类型
    java基础(二) -对象和类
    java基础(一) -语法
    Linux常用命令大全
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10546293.html
Copyright © 2020-2023  润新知