zoukankan      html  css  js  c++  java
  • 329. 矩阵中的最长递增路径 .记忆化dfs

    给定一个整数矩阵,找出最长递增路径的长度。

    对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。

    示例 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]。注意不允许在对角线方向上移动。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class Solution {
    public:
        int dir[4][2] ={-1,0,1,0,0,1,0,-1};
        int n, m;
    
        int longestIncreasingPath(vector<vector<int>>& matrix) {
            if (matrix.empty()) {
                return 0;
            }
            
            n = matrix.size();
            m = matrix[0].size();
    
            auto memo = vector<vector<int>> (n, vector<int>(m, 0));
            int ans = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ans = max(ans, dfs(i, j, memo, matrix));
                }
            }
    
            return ans;
        }
    
        int dfs(int x, int y, vector<vector<int>>& memo, vector<vector<int>>& matrix) {
            if (memo[x][y] != 0) {
                return memo[x][y];
            }
            
            memo[x][y] = 1;
    
            for (int i = 0; i < 4; i++) {
                int tx = x + dir[i][0];
                int ty = y + dir[i][1];
                if (tx >= 0 && tx < n && ty >= 0 && ty < m && matrix[tx][ty] > matrix[x][y]) {
                    memo[x][y] = max(memo[x][y], dfs(tx, ty, memo, matrix) + 1);
                }
            }
            return memo[x][y];
        }
    };
    
    class Solution:
        def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
            if not matrix: return 0
    
            dirs = [(-1,0), (1,0), (0,-1), (0,1)]
            n, m = len(matrix), len(matrix[0])
    
            @lru_cache(None)
            def dfs(x : int, y : int) -> int:
                ret = 1
                for dx, dy in dirs:
                    tx = x + dx
                    ty = y + dy
                    if 0 <= tx < n and 0 <= ty < m and matrix[tx][ty] > matrix[x][y]:
                        ret = max(ret, dfs(tx,ty) + 1)
                return ret
    
            ans = 0
            for i in range(n):
                for j in range(m):
                    ans = max(ans, dfs(i, j)) 
            return ans
    
  • 相关阅读:
    CF819B Mister B and PR Shifts
    HDU5969 最大的位或
    UVA1464 Traffic Real Time Query System
    [SCOI2010]连续攻击游戏
    [USACO11JAN] Roads and Planes
    [POJ3613] Cow Relays(Floyd+矩阵快速幂)
    洛谷P3237 [HNOI2014]米特运输(树形dp)
    awk 正则表达式、正则运算符详细介绍
    awk单行脚本快速参考
    Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
  • 原文地址:https://www.cnblogs.com/xgbt/p/13381040.html
Copyright © 2011-2022 走看看