回档|滑雪

背景

成成第一次模拟赛 第三题
描述

    trs喜欢滑雪。他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形。为了得到更快的速度,滑行的路线必须向下倾斜。
    例如样例中的那个矩形,可以从某个点滑向上下左右四个相邻的点之一。例如24-17-16-1,其实25-24-23…3-2-1更长,事实上这是最长的一条。

输入文件

第1行: 两个数字r,c(1<=r,c<=100),表示矩阵的行列。
第2..r+1行:每行c个数,表示这个矩阵。

输出文件

仅一行: 输出1个整数,表示可以滑行的最大长度。

测试样例1

输入

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
输出

25

题目分析:地图较小,枚举起始点,用DFS枚举最长路径即可AC。

源代码:

const
  dx:array[1..4] of integer=(1,-1,0,0);
  dy:array[1..4] of integer=(0,0,1,-1);
var n,m,i,j,max,ans,t:longint;
  a,f:array[0..100,0..100] of longint;
function dfs(x,y:longint):longint;
var i,xx,yy,t,ans:longint;
begin
  ans:=0;
  if f[x,y]<>0 then exit(f[x,y]);
  for i:=1 to 4 do
    begin
      xx:=x+dx[i];
      yy:=y+dy[i];
      if (xx>=1) and (xx<=n) and (yy>=1) and (yy<=m) and (a[xx,yy]
        begin
          t:=dfs(xx,yy);
          if t>ans then ans:=t;
        end;
    end;
  f[x,y]:=ans+1;
  exit(ans+1);
end;
begin
  readln(n,m);
  for i:=1 to n do
    for j:=1 to m do read(a[i,j]);
  for i:=1 to n do
    begin
      for j:=1 to m do
        begin
          ans:=dfs(i,j);
         if ans>max then max:=ans;
        end;
    end;
  writeln(max);
end.
原文地址:https://www.cnblogs.com/Shymuel/p/4393547.html