https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
一个比较简单的hard...
int px[] = {1,-1,0,0};
int py[] = {0,0,1,-1};
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty() ) return 0;
vector< vector<int> > dp( matrix.size(), vector<int>(matrix[0].size(), 0) );
int ans = 0;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
ans = max(ans, dfs(matrix, dp, i, j) );
}
}
return ans;
}
int dfs(vector< vector<int> > &mat, vector< vector<int> >&dp, int i , int j ) {
// cout << "i:" << i << " j:" << j << endl;
if (i >= mat.size() || j >= mat[0].size()) return 0;
// cout << "i:" << i << " j:" << j << endl;
if (dp[i][j]) return dp[i][j];
int ret = 0;
for (int ii = 0; ii < 4; ii++) {
int xx = i + px[ii];
int yy = j + py[ii];
if (xx >= 0 && xx < mat.size() && yy >= 0 && yy < mat[0].size() && mat[xx][yy] > mat[i][j] ) {
ret = max(ret, dfs( mat, dp, xx, yy ) );
}
}
dp[i][j] = ret + 1;
return dp[i][j];
}
};