【leetcode】329二维矩阵上的最长路径(DP+DFS)

本文提供了一种解决LeetCode 329题“二维矩阵上的最长路径”的算法实现,采用动态规划(DP)结合深度优先搜索(DFS)的方法,详细介绍了代码逻辑与实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【转载】【原文地址】
leetcode329+二维矩阵上的最长路径,DP加上DFS
https://blog.youkuaiyun.com/u013554860/article/details/81320312
题目:
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/


class Solution {
public:
    int dfs(vector<vector<int>>& matrix, vector<vector<int>>& record, int x, int y, int lastVal){
        if(x<0||x>=matrix.size()||y<0||y>=matrix[0].size()) return 0;
        if(matrix[x][y] > lastVal){
            if(record[x][y]!=0) return record[x][y];
            int left = dfs(matrix, record, x, y-1, matrix[x][y])+1;
            int right = dfs(matrix, record, x, y+1, matrix[x][y])+1;
            int top = dfs(matrix, record, x-1, y, matrix[x][y])+1;
            int bottom = dfs(matrix, record, x+1, y, matrix[x][y])+1;
            record[x][y] = max(left, max(right, max(top, bottom)));
            return record[x][y];
        }
        return 0;
    }
    int longestIncreasingPath(vector<vector<int>>& matrix) {
        if(matrix.size()==0) return 0;
        vector<int> temp(matrix[0].size(), 0);
        vector<vector<int>> record(matrix.size(), temp);
        int longest = 0;
        for(int i=0; i<matrix.size(); i++){
            for(int j=0; j<matrix[0].size(); j++){
                longest = max(longest, dfs(matrix, record, i, j, -1));
            }
        }
        return longest;
    }
};
————————————————
版权声明:本文为优快云博主「小堃哥」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013554860/article/details/81320312
爬虫Python学习是指学习如何使用Python编程语言来进行网络爬取和数据提取的过程。Python是一种简单易学且功能强大的编程语言,因此被广泛用于爬虫开发。爬虫是指通过编写程序自动抓取网页上的信息,可以用于数据采集、数据分析、网站监测等多个领域。 对于想要学习爬虫的新手来说,Python是一个很好的入门语言。Python的语法简洁易懂,而且有丰富的第三方库和工具,如BeautifulSoup、Scrapy等,可以帮助开发者更轻松地进行网页解析和数据提取。此外,Python还有很多优秀的教程和学习资源可供选择,可以帮助新手快速入门并掌握爬虫技能。 如果你对Python编程有一定的基础,那么学习爬虫并不难。你可以通过观看教学视频、阅读教程、参与在线课程等方式来学习。网络上有很多免费和付费的学习资源可供选择,你可以根据自己的需求和学习风格选择适合自己的学习材料。 总之,学习爬虫Python需要一定的编程基础,但并不难。通过选择合适的学习资源和不断实践,你可以逐步掌握爬虫的技能,并在实际项目中应用它们。 #### 引用[.reference_title] - *1* *3* [如何自学Python爬虫? 零基础入门教程](https://blog.youkuaiyun.com/zihong523/article/details/122001612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [新手小白必看 Python爬虫学习路线全面指导](https://blog.youkuaiyun.com/weixin_67991858/article/details/128370135)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值