当前位置: 首页 > article >正文

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

题目链接

在这里插入图片描述

思路

方法一:dfs暴力回溯
使用原始used数组+4个方向遍历框架 =, 全局添加一个最大值判断最大的路径长度。
方法二:加上dp数组记忆的优雅回溯
抛弃掉used数组,使用dp数组来记忆遍历过的节点的最长递增路径长度。每遍历到已经记录过的坐标,就直接返回即可。

方法一代码

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

方法二代码

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


http://www.kler.cn/a/454320.html

相关文章:

  • 本地部署 LLaMA-Factory
  • 解决:excel鼠标滚动幅度太大如何调节?
  • (NIPS-2024)PISSA:大型语言模型的主奇异值和奇异向量适配
  • 社区二手物品交易小程序ssm+论文源码调试讲解
  • 如何通过HTTP API插入或更新Doc
  • Android Framework 目录下的 AV/Camera 定制常见问题及解决方法
  • Coding(Jenkinsfile)+ Docker 自动化部署 Springboot —— 图文细节和一些注意事项说明
  • 【NIFI】实现ORACLE->ORACLE数据同步
  • Springboot 整合 Duird
  • 【计算机网络安全】加密解密及其在ssh上的应用
  • 面试场景题系列:设计支付系统
  • UnoCSS 的作用与特点
  • idea配置gitee仓库
  • 讯飞语音听写WebApi(流式)【React Native版】
  • 报警推送消息升级的名厨亮灶开源了。
  • 【Django篇】--动手实践Django基础知识
  • 《Go 语言变量》
  • C语言学习笔记(1)
  • 游戏引擎学习第62天
  • Maven核心概念总结