poj 1088 滑雪 动态规划
题意:n*m矩阵,从某一点起,向上下左右四个方向走,只能走到比自身值小的格子上,问最长路径的长度。
分析:从小到大考虑每一个格子,dp[i][j]=max(dp[i-1][j], dp[i+1][j], dp[i][j+1], dp[i][j-1]) + 1, 其中 a[i][j]>a[x][y]。
int dx[] = {-1,0,1,0};//up Right down Left int dy[] = {0,1,0,-1}; const int M = 105; int a[M][M], d[M][M]; int n, m, ans; struct node{ int x,y; }nd[M*M]; int p; bool cmp(node n1, node n2){ return a[n1.x][n1.y]<a[n2.x][n2.y]; } int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif scanf("%d%d", &n, &m); FOR(i, 0, n) FOR(j, 0, m){ scanf("%d", &a[i][j]); nd[p].x=i; nd[p++].y=j; d[i][j]=1; } sort(nd, nd+p, cmp); ans = 1; FOR(k, 0, p){ int i=nd[k].x, j=nd[k].y; FOR(r, 0, 4){ int ii=i+dx[r], jj=j+dy[r]; if(ii<0 || ii>=n || jj<0 || jj>=m) continue; if(a[i][j] <= a[ii][jj] || d[i][j] >= d[ii][jj]+1) continue; d[i][j] = d[ii][jj]+1; } checkmax(ans, d[i][j]); } printf("%d\n", ans); return 0; }