这是在线评判员, https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
为什么我无法使用DFS获得结果?
当你从每个细胞中获知时,要么向四个方向移动:左,右,上或下。
存储最长路径的长度。
/*
for each elem, neighbours dfs
*/
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
int x[] = {0,1,0,-1};// l-r -1,1
int y[] = {1,0,-1,0};// up-down +1,-1
int maxlen = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j< col; j++){
// each node in the matrix[i][j], neighbours
int len = 0;
dfs(maxlen, len, i, j, x, y, matrix);
}
}
return maxlen;
}
private:
bool isIn(int x, int y, int row, int col){
if(x>=0&&x<=col && y>=0&&y<=row) return true;
else return false;
}
void dfs(int& maxlen, int len, int i, int j,int* x, int* y, vector<vector<int>> matrix){
int row = matrix.size();
int col = matrix[0].size();
for(int k = 0; k < 4; k++){
int i_t = i+x[k];//the current position
int j_t = j+y[k];
if(isIn(i_t,j_t,row,col)&& (matrix[i_t][j_t]>matrix[i][j]) ){ // if inside the matrix, within the boundary&& the value of (i_t,j_t)>
len+=1;
maxlen = max(len,maxlen);
dfs(maxlen, len, i_t, j_t, x, y, matrix);
}
}
}
};
答案 0 :(得分:0)
此代码存在多个问题。
if(x>=0&&x<=col && y>=0&&y<=row)
应更改为if(x>=0&&x<col && y>=0&&y<row)
您正在将所有源自一个元素的路径添加到一起,这会导致错误的答案。 这部分代码
len+=1;
maxlen = max(len,maxlen);
dfs(maxlen, len, i_t, j_t, x, y, matrix);
应更改为:
//len+=1;
maxlen = max(len+1,maxlen);
dfs(maxlen, len+1, i_t, j_t, x, y, matrix);
这样您就不会将所有路径添加到不同的方向。
dfs(r,c)
后,您可以保存其结果,并将该值用于将来的参考(动态编程)。这就是我实现它的方式:
#include <vector>
#include <iostream>
#include <map>
using namespace std;
map< pair<int,int>, int > dp;
pair<int,int> moves[] = {{0,1},{0,-1},{1,0},{-1,0}};
vector<vector<int> > matrix = { {3,4,5},
{3,2,6},
{2,2,1}};
int dfs(int r, int c, int n_rows, int n_cols){
pair<int,int> p = make_pair(r,c);
if ( dp.count(p) ){
return dp[p];
}
int mx = 0;
for ( int i=0; i<4; ++i ){
int next_r = r+moves[i].first;
int next_c = c+moves[i].second;
if ( 0<=next_r && next_r < n_rows && 0<=next_c && next_c < n_cols ){
if ( matrix[next_r][next_c] > matrix[r][c] )
mx = max(mx, dfs(next_r, next_c, n_rows, n_cols));
}
}
mx++;
dp[p] = mx;
return mx;
}
int main(){
int rows = matrix.size();
int cols = matrix[0].size();
int result = 0;
for ( int i=0; i<rows; ++i ){
for ( int j=0; j<cols; ++j ){
result = max(result, dfs(i,j,rows,cols));
}
}
cout << result << endl;
}
答案 1 :(得分:0)
这是我使用DFS +备注化的解决方案
class Solution {
int r;
int c;
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
r = matrix.size();
if(r == 0) return 0;
c= matrix[0].size();
int maxlength = 0;
vector<vector<int>> dfs(r, vector<int>(c, 0));
for(int i = 0; i < r; ++i) {
for(int j = 0 ; j < c; ++j) {
int curr = recursive(i,j, dfs, matrix);
maxlength = max(maxlength, curr);
}
}
return maxlength;
}
int recursive(int i, int j, vector<vector<int>>& dfs, vector<vector<int>>& matrix) {
if(dfs[i][j] != 0) return dfs[i][j];
else {
int maxi = 1;
// b.c 1
if (i-1 >= 0 && (matrix[i-1][j]>matrix[i][j])) {
maxi = max(maxi,1+ recursive(i-1, j, dfs, matrix));
}
// b.c 2
if (j -1 >=0 && (matrix[i][j-1]>matrix[i][j])) {
maxi = max(maxi,1+ recursive(i, j-1, dfs, matrix));
}
// b.c 3
if (i+1 < r && (matrix[i+1][j]>matrix[i][j])) {
maxi = max(maxi,1+ recursive(i+1, j, dfs, matrix));
}
// b.c. 4
if(j+1 < c && (matrix[i][j+1]>matrix[i][j])) {
maxi = max(maxi,1+ recursive(i, j+1, dfs, matrix));
}
dfs[i][j] = maxi;
return maxi;
}
}
};