[ python ] 动态规划笔记 [三] 坐标型动态规划

坐标型动态规划

· 给定一个序列或者网格,需要找到序列中某个/些子序列或者网格中的某条路径
1、某种性质最大或者最小
2、计数
3、存在性

初始条件f[0]就是指以a0为结尾的子序列的性质
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class Solution:
    def calculate(self, lis):
        n = len(lis)
        dp = 1
        max1 = 1
        for i in range(1, n):
            if lis[i] > lis[i-1]:
                dp += 1
                max1 = max(dp, max1)
            else:
                dp = 1
        return max1

在这里插入图片描述

class Solution:
    def calculate(self, grid):
        if grid == []:
            return 0
        m, n = len(grid), len(grid[0])
        f = [[0] * n for _ in range(m)]
        #f[m-1][n-1] = min(f[m-2][n-1] + grid[m-1][n-1], f[m-1][n-2] + grid[m-1][n-1])
        for i in range(0, m):
            for j in range(0, n):
                if j == 0 and i == 0:
                    f[i][j] = grid[0][0]
                elif i == 0 and j != 0:
                    f[i][j] = f[i][j-1] + grid[i][j]
                elif i != 0 and j == 0:
                    f[i][j] = f[i-1][j] + grid[i][j]
                else:
                    f[i][j] = min(f[i][j-1] + grid[i][j], f[i-1][j] + grid[i][j])
        return f[m-1][n-1]

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

def maxKilledEnemies(grid):
	#炸弹袭击,动态规划
    # write your code here
    if grid is None or len(grid) == 0 or len(grid[0]) == 0:
        return 0
    m = len(grid)
    n = len(grid[0])
    dp = [[0] * (n) for i in range(m)]
    res = [[0] * (n) for i in range(m)]
    result = 0
    #向上
    for i in range(0, m):
        for j in range(0, n):
            if grid[i][j] == 'W':
                dp[i][j] = 0
            else:
                dp[i][j] = 0
                if grid[i][j] == 'E':
                    dp[i][j] = 1
            #这句判定条件很重要
                if i > 0:
                    dp[i][j] += dp[i-1][j]

            res[i][j] += dp[i][j]

    #向下
    for i in range(m-1, -1, -1):
        for j in range(0, n):
            if grid[i][j] == 'W':
                dp[i][j] = 0
            else:
                dp[i][j] = 0
                if grid[i][j] == 'E':
                    dp[i][j] = 1
                if i < m-1:
                    dp[i][j] += dp[i+1][j]

            res[i][j] += dp[i][j]

    # 向左
    for i in range(0, m):
        for j in range(0, n, 1):
            if grid[i][j] == 'W':
                dp[i][j] = 0
            else:
                dp[i][j] = 0
                if grid[i][j] == 'E':
                    dp[i][j] = 1
                if j > 0:
                    dp[i][j] += dp[i][j-1]

            res[i][j] += dp[i][j]

    # 向右
    for i in range(0, m):
        for j in range(n-1, -1, -1):
            if grid[i][j] == 'W':
                dp[i][j] = 0
            else:
                dp[i][j] = 0
                if grid[i][j] == 'E':
                    dp[i][j] = 1
                if j < n-1:
                    dp[i][j] += dp[i][j+1]

            res[i][j] += dp[i][j]

    for i in range(m):
        for j in range(n):
            #只能放在空地
            if grid[i][j] == '0':
                result = max(result, res[i][j])
    return result
grid = [['0', 'E', '0', '0'], ['E', '0', 'W', 'E'], ['0', 'E', '0', '0']]
print(maxKilledEnemies(grid))

在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页