# 矩阵中的最长递增路径

给定一个 m x n 整数矩阵 matrix ,找出其中 最长递增路径 的长度。

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

 

示例 1:

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

示例 2:

输入:matrix = [[3,4,5],[3,2,6],[2,2,1]]
输出:4 
解释:最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。

示例 3:

输入:matrix = [[1]]
输出:1

 

提示:

## template ```java class Solution { public int longestIncreasingPath(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return 0; } int max = 0; int row = matrix.length; int col = matrix[0].length; int[][] dp = new int[row][col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { max = Math.max(max, loop(matrix, Integer.MIN_VALUE, dp, i, j)); } } return max; } private int loop(int[][] mat, int pre, int[][] dp, int i, int j) { if (i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || mat[i][j] <= pre) { return 0; } if (dp[i][j] != 0) { return dp[i][j]; } int max = 0; max = Math.max(max, loop(mat, mat[i][j], dp, i - 1, j)); max = Math.max(max, loop(mat, mat[i][j], dp, i + 1, j)); max = Math.max(max, loop(mat, mat[i][j], dp, i, j - 1)); max = Math.max(max, loop(mat, mat[i][j], dp, i, j + 1)); dp[i][j] = max + 1; return dp[i][j]; } } ``` ## 答案 ```java ``` ## 选项 ### A ```java ``` ### B ```java ``` ### C ```java ```