(补)算法刷题Day24: BM61 矩阵最长递增路径

题目链接

思路

方法一:dfs暴力回溯

使用原始used数组+4个方向遍历框架 =, 全局添加一个最大值判断最大的路径长度。

方法二:加上dp数组记忆的优雅回溯

抛弃掉used数组,使用dp数组来记忆遍历过的节点的最长递增路径长度。每遍历到已经记录过的坐标,就直接返回即可。

方法一代码

python 复制代码
import copy
max_result_len = -1
result = []
direct = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def dfs(matrix, used, row_n, col_m, x, y, path):
    # 判断是否合法
    global max_result_len
    global result
    if len(path) > max_result_len:
        max_result_len = len(path)
        print(max_result_len)
        print(path)
        result = copy.deepcopy(path)
    if x < 0 or y < 0 or x >= row_n or y >= col_m:
        return
    if used[x][y]:
        return
    # 如果当前节点值是小于前一个,则pass
    if matrix[x][y] <= path[-1]:
        return
    used[x][y] = True
    path.append(matrix[x][y])
    for dx, dy in direct:
        nx = x + dx
        ny = y + dy
        dfs(matrix, used, row_n, col_m, nx, ny, path)
    used[x][y] = False
    path.pop()
class Solution:
    def solve(self, matrix: List[List[int]]) -> int:
        # write code here
        row = len(matrix)
        col = len(matrix[0])
        used = [[False for _ in range(row)] for _ in range(col)]
        for i in range (row):
            for j in range (col):
                dfs(matrix, used, row, col, i, j, [-1])
        return max_result_len-1

方法二代码

python 复制代码
direct = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def dfs(matrix, row_n, col_m, x, y, path,dp):
    # 判断是否合法
    if x < 0 or y < 0 or x >= row_n or y >= col_m:
        return 0
    # 如果当前节点值是小于前一个,则pass
    if matrix[x][y] <= path[-1]:
        return 0
    # 如果 dp 记录过就直接加上
    if dp[x][y] != -1:
        return dp[x][y]
    path.append(matrix[x][y])
    my_max = -1
    for dx, dy in direct:
        nx = x + dx
        ny = y + dy
        sub_max = dfs(matrix, row_n, col_m, nx, ny, path,dp)
        my_max = max(sub_max,my_max)
    path.pop()
    dp[x][y] = my_max+1
    return my_max+1
class Solution:
    def solve(self, matrix: List[List[int]]) -> int:
        row = len(matrix)
        col = len(matrix[0])
        dp = [[-1 for _ in range(row)]for _ in range(col)]
        max_result_len = -1
        for i in range(row):
            for j in range(col):
                m = dfs(matrix,row, col, i, j, [-1],dp)
                max_result_len = max(max_result_len, m)
        return max_result_len

这道题的dp卡了我很久。让我好几天都没有刷题的欲望。在需要机械化完成的任务面前,情绪更多时候真的是没用的东西。反正都要做的,早做晚做都是要做,开心也要做不开心也要做,倒不如不怀情绪地认真做。别急~

相关推荐
fsnine15 小时前
Python Web框架对比与模型部署
开发语言·前端·python
微笑尅乐15 小时前
从递归到迭代吃透树的层次——力扣104.二叉树的最大深度
算法·leetcode·职场和发展
im_AMBER15 小时前
Leetcode 28
算法·leetcode
杰 .15 小时前
C++ Hash
数据结构·c++·哈希算法
让我们一起加油好吗15 小时前
【基础算法】多源 BFS
c++·算法·bfs·宽度优先·多源bfs
B站计算机毕业设计之家15 小时前
深度学习实战:python动物识别分类检测系统 计算机视觉 Django框架 CNN算法 深度学习 卷积神经网络 TensorFlow 毕业设计(建议收藏)✅
python·深度学习·算法·计算机视觉·分类·毕业设计·动物识别
程序猿小D16 小时前
【完整源码+数据集+部署教程】 【运输&加载码头】仓库新卸物料检测系统源码&数据集全套:改进yolo11-DRBNCSPELAN
python·yolo·计算机视觉·目标跟踪·数据集·yolo11·仓库新卸物料检测系统
韧竹、16 小时前
数据结构之单链表
数据结构·链表
And_Ii16 小时前
LeetCode 3350. 检测相邻递增子数组 II
数据结构·算法·leetcode
想唱rap16 小时前
C++ string类的使用
开发语言·c++·笔记·算法·新浪微博