In fact , the posted solution is really dirty .... Here is a much better coding style implementation ...

class Solution {
const vector<vector<int>> move = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
if(matrix.size()==0) return 0;
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> cache(m, vector<int>(n, 0));
int result = 1;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
int len = dfs(matrix, i, j, m, n, cache);
result = max(result, len);
}
}
return result;
}
int dfs(vector<vector<int>>& matrix, int i, int j, int m, int n, vector<vector<int>>& cache){
if(cache[i][j] != 0) return cache[i][j];
int result = 1;
for(auto dir : move) {
int x = i + dir[0], y = j + dir[1];
if(x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] <= matrix[i][j]) continue;
int len = 1 + dfs(matrix, x, y, m, n, cache);
result = max(result, len);
}
cache[i][j] = result;
return result;
}
};