滑雪(动态规划)

来源:互联网 发布:儿童网络暴力 编辑:程序博客网 时间:2024/05/04 14:00

Description

Michael喜欢滑雪百这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 


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 

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

Input

输入的第一行表示区域的行数R和列数C(1 <=R,C <=100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。 

Output

输出最长区域的长度。

SampleInput

 
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

SampleOutput

 

25
 
 
解题思路:
f[i,j]表示到[i,j]为止的最大长度,状态转移方程为:
f[xx,yy]:=f[a[i].x,a[i].y]+1;
时间复杂度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;
     end;
var
  map,f:array[1..maxn,1..maxn] of integer;
  a:array[1..maxn*maxn] of arr;
  r,c,sum,i,j,k,xx,yy:longint;

procedure qsort(l,r:longint);
  var
   i,j,mid:longint;
    t:arr;
  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 do dec(j);
     if i<=j then
       begin
          t:=a[i];
          a[i]:=a[j];
          a[j]:=t;
          inc(i);
          dec(j);
       end;
    untili>=j;
   qsort(l,j);
   qsort(i,r);
end;

begin
  readln(r,c);
  k:=0;
  sum:=0;
  for i:=1 to r do
    for j:=1 toc do
     begin
       read(map[i,j]);
       inc(k);
       a[k].x:=i;
       a[k].y:=j;
       a[k].s:=map[i,j];
     end;
  qsort(1,k);
  for i:=1 to k do
    for j:=1 to4 do
     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<=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]:=f[a[i].x,a[i].y]+1;
             if f[xx,yy]>sum then sum:=f[xx,yy];
           end;
     end;
  writeln(sum+1);
end.

 
 
版权属于: Chris

原文地址: http://blog.sina.com.cn/s/blog_83ac6af80102vjrv.html

转载时必须以链接形式注明原始出处及本声明。

0 1