using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 递增路径的最大长度 * @param matrix int整型二维数组 描述矩阵的每个数 * @return int整型 */ int[,] memory; List<List<int>> matrix; int m,n; public int solve (List<List<int>> matrix) { this.matrix = matrix; m = matrix.Count; n = matrix[0].Count; memory = new int[m,n]; int res = 0; for(int i = 0; i < m; i++){ for(int j = 0; j < n;j++){ res = Math.Max(res, Search(i,j)); //Console.WriteLine(memory[i,j]); } } return res; } public int Search(int x, int y){ if(memory[x,y] != 0) return memory[x,y]; memory[x,y] = 1; if(x > 0 && matrix[x][y] < matrix[x - 1][y]) memory[x,y] = Math.Max(memory[x,y],Search(x - 1, y)+1); if(y > 0 && matrix[x][y] < matrix[x][y - 1]) memory[x,y] = Math.Max(memory[x,y],Search(x, y - 1)+1); if(x < m-1 && matrix[x][y] < matrix[x + 1][y]) memory[x,y] = Math.Max(memory[x,y],Search(x + 1, y)+1); if(y < n-1 && matrix[x][y] < matrix[x][y + 1]) memory[x,y] = Math.Max(memory[x,y],Search(x, y + 1)+1); return memory[x,y]; } }