https://leetcode.cn/problems/longest-increasing-path-in-a-matrix/submissions/577581299/
记忆化的数组存的是:dp[i][j],在i,j这个点出发能得到的最长递增路径
const int N=210;
int dp[N][N];
int a[N][N];
int n,m;
int dfs(int x,int y)
{
if(dp[x][y]!=0) return dp[x][y];
int ans=0;
if(x-1>=0&&a[x-1][y]>a[x][y]) ans=max(ans,dfs(x-1,y));
if(x+1<n&&a[x+1][y]>a[x][y]) ans=max(ans,dfs(x+1,y));
if(y-1>=0&&a[x][y-1]>a[x][y]) ans=max(ans,dfs(x,y-1));
if(y+1<m&&a[x][y+1]>a[x][y]) ans=max(ans,dfs(x,y+1));
dp[x][y]=ans+1;
return dp[x][y];
}
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
n=matrix.size();
m=matrix[0].size();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
dp[i][j]=0,a[i][j]=matrix[i][j];
int res=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
res=max(res,dfs(i,j));
}
}
return res;
}
};