原题链接:http://poj.org/problem?id=1088
动态规划,记忆化搜索。
View Code
#include <cstdio> #include <cstring> #define MAXN 105 #define max(x,y) x > y ? x : y int a[MAXN][MAXN], dp[MAXN][MAXN]; int dr[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; int R, C; int DP(int r, int c) { if(dp[r][c] != -1) { return dp[r][c]; } dp[r][c] = 1; //initialize for(int i = 0; i < 4; i ++) { int rr = r + dr[i][0]; int cc = c + dr[i][1]; if(rr < 1 || rr > R || cc < 1 || cc > C) continue; if(a[rr][cc] < a[r][c]) dp[r][c] = max(dp[r][c], DP(rr, cc) + 1); } return dp[r][c]; } void skiing() { memset(dp, -1, sizeof(dp)); for(int r = 1; r <= R; r ++) { for(int c = 1; c <= C; c ++) { dp[r][c] = DP(r, c); } } } int main() { while(~scanf("%d%d", &R, &C)) { for(int i = 1; i <= R; i ++) { for(int j = 1; j <= C; j ++) { scanf("%d", &a[i][j]); } } skiing(); int max = 0; for(int i = 1; i <= R; i ++) { for(int j = 1; j <= C; j ++) { if(dp[i][j] > max) max = dp[i][j]; } } printf("%d\n", max); } return 0; }