# 矩阵中的最长递增路径

给定一个 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

提示:

以下错误的选项是?

## aop ### before ```c #include using namespace std; ``` ### after ```c ``` ## 答案 ```c class Solution { public: int m, n; vector> memo; int dfs(vector> &matrix, int x, int y) { if (memo[x][y] != -1) return memo[x][y]; int ret = 1; if (x > 0 && matrix[x - 1][y] > matrix[x][y]) ret = max(ret, 1 + dfs(matrix, x - 1, y - 1)); if (x < m - 1 && matrix[x + 1][y] > matrix[x][y]) ret = max(ret, 1 + dfs(matrix, x + 1, y + 1)); if (y > 0 && matrix[x][y - 1] > matrix[x][y]) ret = max(ret, 1 + dfs(matrix, x, y - 1)); if (y < n - 1 && matrix[x][y + 1] > matrix[x][y]) ret = max(ret, 1 + dfs(matrix, x, y + 1)); memo[x][y] = ret; return ret; } int longestIncreasingPath(vector> &matrix) { m = matrix.size(); if (m == 0) return 0; n = matrix[0].size(); memo.resize(m); int ans = 1; for (int i = 0; i < m; ++i) memo[i].resize(n, -1); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { int temp = dfs(matrix, i, j); ans = ans < temp ? temp : ans; } } return ans; } }; ``` ## 选项 ### A ```c class Solution { public: vector> state = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int longestIncreasingPath(vector> &matrix) { int n = matrix.size(); if (n == 0) return 0; int m = matrix[0].size(); vector> dp(n, vector(m, 0)); int res = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) res = max(dfs(dp, matrix, i, j), res); return res; } int dfs(vector> &dp, vector> matrix, int i, int j) { if (dp[i][j] != 0) return dp[i][j]; dp[i][j] = 1; for (vector s : state) { int x = i + s[0]; int y = j + s[1]; if (x >= 0 && x < matrix.size() && y >= 0 && y < matrix[0].size() && matrix[i][j] < matrix[x][y]) dp[i][j] = max(dp[i][j], dfs(dp, matrix, x, y) + 1); } return dp[i][j]; } }; ``` ### B ```c class Solution { public: vector> use; int dfs(int i, int j, vector> &matrix, int m, int n) { int len = 1, size, ans = 0; if (i - 1 >= 0 && matrix[i - 1][j] > matrix[i][j]) { if (use[i - 1][j] == 0) len = max(len, dfs(i - 1, j, matrix, m, n) + 1); else len = max(len, use[i - 1][j] + 1); } if (j - 1 >= 0 && matrix[i][j - 1] > matrix[i][j]) { if (use[i][j - 1] == 0) len = max(len, dfs(i, j - 1, matrix, m, n) + 1); else len = max(len, use[i][j - 1] + 1); } if (i + 1 < m && matrix[i + 1][j] > matrix[i][j]) { if (use[i + 1][j] == 0) len = max(len, dfs(i + 1, j, matrix, m, n) + 1); else len = max(len, use[i + 1][j] + 1); } if (j + 1 < n && matrix[i][j + 1] > matrix[i][j]) { if (use[i][j + 1] == 0) len = max(len, dfs(i, j + 1, matrix, m, n) + 1); else len = max(len, use[i][j + 1] + 1); } use[i][j] = len; return len; } int longestIncreasingPath(vector> &matrix) { if (matrix.empty() || matrix[0].empty()) return 0; int m = matrix.size(), n = matrix[0].size(); use = vector(m, vector(n, 0)); int i, j; int ans = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (!use[i][j]) ans = max(ans, dfs(i, j, matrix, m, n)); } } return ans; } }; ``` ### C ```c class Solution { public: static constexpr int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int m, n; int longestIncreasingPath(vector> &matrix) { if (matrix.size() == 0 || matrix[0].size() == 0) { return 0; } m = matrix.size(); n = matrix[0].size(); int res = 0; auto memo = vector>(m, vector(n, 0)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (memo[i][j]) res = max(res, memo[i][j]); else res = max(res, dfs(i, j, matrix, memo)); } } return res; } int dfs(int i, int j, vector> &matrix, vector> &memo) { int temp = 1; for (int k = 0; k < 4; ++k) { int x = i + dirs[k][0]; int y = j + dirs[k][1]; if ((x >= 0) && (x < m) && (y >= 0) && (y < n) && (matrix[i][j] < matrix[x][y])) { if (memo[x][y]) temp = max(temp, memo[x][y] + 1); else temp = max(temp, dfs(x, y, matrix, memo) + 1); } } memo[i][j] = temp; return temp; } }; ```