Skiing (dynamic planning) __ Dynamic planning

Source: Internet
Author: User


It's not surprising that Michael likes skiing, because skiing is really exciting. But in order to get the speed, the slippery area must be tilted downward, and when you slide to the bottom of the slope, you have to go uphill again or wait for the lift to load you. Michael wants to know the longest bottom slide in a region. The region is given by a two-dimensional array. Each digit of the array represents the height of the point. Here is an example

1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

A person can slide from one point to the next four or so adjacent points, when and only if the height is reduced. In the example above, a sliding landslide is 24-17-16-1. Of course 25-24-23-...-3-2-1 longer. In fact, this is the longest one.


The first line you enter represents the number of rows in the range R and the number of columns C (1 <=r,c <=100). Here are the r rows, each with a C integer representing a height of h,0<=h<=10000.


The length of the longest area of output.


5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9


Ideas for solving problems:
F[I,J] represents the maximum length until [i,j], and the state transfer equation is:
Time complexity O (n㏒n)

Const MAXN=500;
WAY:ARRAY[1..4,1..2] oflongint= (( -1,0), (1,0), (0,-1), (0,1));
     Type Arr=record X,y,s:integer;
  var map,f:array[1..maxn,1..maxn] of Integer;
  A:ARRAY[1..MAXN*MAXN] of arr;
  Procedure Qsort (L,r:longint);
    var i,j,mid:longint;
    Begin if L>=rthen exit; I:=l;
    j:=r;mid:=a[(l+r) div 2].s;
     Repeat while A[I].S while A[j].s>mid does Dec (j);
          If I<=j then begin t:=a[i];
          Inc (I);
       Dec (j);
   Qsort (L,J);
Qsort (I,R);
  Begin READLN (R,C);
       For I:=1 to R doing for J:=1 TOC do begin read (MAP[I,J));
       Inc (k);
  Qsort (1,K);
       For I:=1 to K does for J:=1 to4 does begin xx:=a[i].x+way[j,1];
       yy:=a[i].y+way[j,2]; if (xx>0) and (Xx<=r) and (yy>0) and (YY&LT;=C) then if (F[a[i].x,a[i].y]+1>f[xx,yy)) and (Map[a[i].x,a[i].y] begin F[XX,YY
           If F[xx,yy]>sum then Sum:=f[xx,yy];
Writeln (sum+1); End.

Copyright belongs to: Chris

Original address:

Reprint must be linked to the form of the original source and this statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.