状态转移方程dp(i, j) = max(dp(i - 1, j), dp(i, (j- 1), dp(i + 1, j), dp(i, j + 1)) + 1; #include <cstdlib> #include <iostream> using namespace std; const int MAX = 101; int dp[MAX][MAX] = {{0, 0}}; int height[MAX][MAX]; int getMax(int i, int j, int R, int C) { int max = 1; if(dp[i][j] != 0) return dp[i][j]; if(i < 0 || j < 0 || i >= R || j >= C) return 0; if(height[i][j] > height[i - 1][j]) { int len = getMax(i - 1, j, R, C) + 1; if(len > max) max = len; } if(height[i][j] > height[i][j - 1]) { int len = getMax(i, j - 1, R, C) + 1; if(len > max) max = len; } if(height[i][j] > height[i + 1][j]) { int len = getMax(i + 1, j, R, C) + 1; if(len > max) max = len; } if(height[i][j] > height[i][j + 1]) { int len = getMax(i, j + 1, R, C) + 1; if(len > max) max = len; } return max; } int main(int argc, char *argv[]) { //freopen("input.txt", "rt", stdin); //freopen("output.txt", "wt", stdout); int R, C; cin >> R >> C; for(int i=0; i<R; i++) for(int j=0; j<C; j++) cin >> height[i][j]; int max = 0; for(int i=0; i<R; i++) { for(int j=0; j<C; j++) { dp[i][j] = getMax(i, j, R, C); if(dp[i][j] > max) max = dp[i][j]; } } cout << max << endl; return EXIT_SUCCESS; }