(补)算法刷题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卡了我很久。让我好几天都没有刷题的欲望。在需要机械化完成的任务面前,情绪更多时候真的是没用的东西。反正都要做的,早做晚做都是要做,开心也要做不开心也要做,倒不如不怀情绪地认真做。别急~

相关推荐
lxmyzzs18 分钟前
【图像算法 - 16】庖丁解牛:基于YOLO12与OpenCV的车辆部件级实例分割实战(附完整代码)
人工智能·深度学习·opencv·算法·yolo·计算机视觉·实例分割
R-G-B41 分钟前
OpenCV Python——Numpy基本操作(Numpy 矩阵操作、Numpy 矩阵的检索与赋值、Numpy 操作ROI)
python·opencv·numpy·numpy基本操作·numpy 矩阵操作·numpy 矩阵的检索与赋值·numpy 操作roi
细节处有神明1 小时前
Jupyter 中实现交互式图表:ipywidgets 从入门到部署
ide·python·jupyter
小小码农一只1 小时前
Python 爬虫实战:玩转 Playwright 跨浏览器自动化(Chromium/Firefox/WebKit 全支持)
爬虫·python·自动化
wow_DG1 小时前
【C++✨】多种 C++ 解法固定宽度右对齐输出(每个数占 8 列)
开发语言·c++·算法
Epiphany.5561 小时前
c++最长上升子序列长度
c++·算法·图论
Cx330❀2 小时前
【数据结构初阶】--排序(四):归并排序
c语言·开发语言·数据结构·算法·排序算法
深盾安全2 小时前
Python脚本安全防护策略全解析(上)
python
余_弦2 小时前
区块链中的密码学 —— 密钥派生算法
算法·区块链
杜子不疼.2 小时前
《Python学习之使用标准库:从入门到实战》
开发语言·python·学习