矩阵最长递增路径
深搜+dp即可。
private int[][] dirs=new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
private int m,n;
public int solve (int[][] matrix) {
// write code here
if(matrix.length==0||matrix[0].length==0) return 0;
m=matrix.length;
n=matrix[0].length;
int res=0;
int[][] dp=new int[m+1][n+1];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
res=Math.max(res,dfs(matrix,dp,i,j));
}
}
return res;
}
public int dfs(int[][] matrix,int[][] dp,int i,int j){
if(dp[i][j]!=0) return dp[i][j];
dp[i][j]++;
for (int k=0;k<4;k++){
int nexti=i+dirs[k][0];
int nextj=j+dirs[k][1];
if(nexti>=0&&nexti<m&&nextj>=0&&nextj<n&&matrix[nexti][nextj]>matrix[i][j]){
dp[i][j]=Math.max(dp[i][j],dfs(matrix,dp,nexti,nextj));
}
}
return dp[i][j];
}