LeetCode. 矩阵中的最长递增路径

题目要求:

给定一个整数矩阵,找出最长递增路径的长度。
对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。

示例:

输入: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]]
输出: 4
解释: 最长递增路径为 [1, 2, 6, 9]。


class Solution {
public:
    int dx[5] = {-1, 0, 1, 0};
    int dy[5] = {0, 1, 0, -1};

    int longestIncreasingPath(vector<vector<int>>& matrix) {
        if(matrix.empty()) return 0;
        int max1 = 0;
        vector<vector<bool>> visited(matrix.size()+1, vector<bool>(matrix[0].size()+1, false));
        vector<vector<int>> len(matrix.size()+1, vector<int>(matrix[0].size()+1, 0));
        for(int i = 0; i < matrix.size(); i++) {
            for(int j = 0; j < matrix[0].size(); j++) {
                max1 = max(max1, find(matrix, visited, len, i, j));
            }
        }
        return max1;
    }

    int find(vector<vector<int>> matrix, vector<vector<bool>> visited, vector<vector<int>> len, int x, int y) {
        if(visited[x][y]) {
            return len[x][y];
        }
        len[x][y] = 1;
        for(int i = 0; i < 4; i++) {
            int _x = dx[i] + x;
            int _y = dy[i] + y;
            if(_x >= 0 && _x < matrix.size() && _y >= 0 && _y < matrix.size() && matrix[_x][_y] < matrix[x][y]) {
                len[x][y] = max(len[x][y], find(matrix, visited, len, _x, _y) + 1);
            }
        }
        visited[x][y] = true;
        return len[x][y];
    }
};

原文地址:https://www.cnblogs.com/leyang2019/p/11680086.html

时间: 2024-07-31 12:53:01

LeetCode. 矩阵中的最长递增路径的相关文章

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

[Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed). E

329. 矩阵中的最长递增路径

题目: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1]] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].示例 2: 输入: nums = [ [3,4,5], [3,2,6], [2,2,1]] 输出: 4 解释: 最长递增路径是 [3, 4, 5, 6].注意不允许在对角线方向上移动

【python-leetcode329-深度优先搜索】矩阵中的最长递增路径

给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1]] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].示例 2: 输入: nums = [ [3,4,5], [3,2,6], [2,2,1]] 输出: 4 解释: 最长递增路径是 [3, 4, 5, 6].注意不允许在对角线方向上移动. cl

329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径

Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).Exa

矩阵中的最长递增子序列

和上一道  最长上升子序列 思路一样,不过还是从最笨的方法开始吧,也算是记录一下思考过程. 最开始的想法是,对矩阵的每个点都来一次回溯,得到从这个点开始的最长上升子序列长度.回溯的思路就是对上下左右遍历,利用到递归,停止遍历的条件是四周的数都比它大.代码如下: class Solution1 { public: // 对矩阵的每个点直接利用回溯法 超时了 void FindMaxLength(vector< vector<int> >& matrix, vector<

51nod1274 最长递增路径

将边排序后dp一下就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #define clr(x,c) memset(x,c,sizeof(

矩形网格中寻找最长递增序列

在矩形网格中寻找最长的递增序列 比如如下网格 97,47,56,36 35,57,41,13 89,36,98,75 25,45,26,17 结果要求输出 17, 26, 36, 41, 47, 56, 57, 97 基本想法就是对图中的每一个点都当作起始点试一编 将序列最长的保存起来 最后输出 代码如下 使用java编写 import java.util.ArrayList; public class 最长递增序列 { static int[][] rect={ {97,47,56,36},

求一个有向无换图中,最长简单路径。动态规划问题15-1

package com.li.chapter15.Prictice; /** * 练习题1: 求有向无环图两点路径的最大值. * * 思路: 给定有向无环图的矩阵, 从1-n的最长距离,2-n的最长距离... * * maxLength[i,j]=maxlenth[i,j-1]+length[j] //length[j]是与终点相连的很多点之一.最大的一个. */ public class Question01 { public static void main(String[] args){