codevs 2152 滑雪

2152 滑雪

 

 时间限制: 1 s
 空间限制: 32000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

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

输入描述 Input Description

输入文件

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

输出描述 Output Description

输出文件

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

样例输入 Sample Input

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

样例输出 Sample Output

25

数据范围及提示 Data Size & Hint

1s

 
 
#include<cstdio>
#include<iostream>
using namespace std;
int ans=0,r,c,f[200][200]={0},a[200][200];
int dx[5]={0,0,-1,0,1},dy[5]={0,1,0,-1,0};
//深搜寻找最长的路 
int dfs(int x,int y)
{
    int nx,ny;
    if (f[x][y])
      return (f[x][y]);
    for (int i=1;i<=4;i++)//搜寻上下左右四个方向 
      {
           nx=x+dx[i];
           ny=y+dy[i]; 
           if ((nx>=1)&&(nx<=r)&&(ny>=1)&&(ny<=c)&&(a[x][y]>a[nx][ny]))//这里也可以用a[x][y]<a[nx][ny],那就是倒推找从低到高最长的一条,本质都是最长的一条路 
            f[x][y]=max(f[x][y],dfs(nx,ny)+1);
      }
    return (f[x][y]);
}
int main()
{
    scanf("%d%d",&r,&c);
    for (int i=1;i<=r;i++)
      for (int j=1;j<=c;j++)
        scanf("%d",&a[i][j]);
    for (int i=1;i<=r;i++)
      for (int j=1;j<=c;j++)
        {
            f[i][j]=dfs(i,j);
            ans=max(ans,f[i][j]+1);//由于每一个f内为加它的起点,因此+1            
        }

    printf("%d",ans);
 } 
I'm so lost but not afraid ,I've been broken and raise again
原文地址:https://www.cnblogs.com/sjymj/p/5303582.html