滑雪(记忆化搜索)

滑雪

题目限制

时间限制 内存限制 评测方式 题目来源
1000ms 131072KiB 标准比较器 Local

题目背景

成成第一次模拟赛 第三道

题目描述

    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 输出样例 #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
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define ll long long;
 6 
 7 using namespace std;
 8 
 9 int n,m;
10 int arr[105][105];
11 int fx[4][2]={0,1,0,-1,1,0,-1,0};
12 int dp[105][105];
13 
14 int dfs(int x,int y){
15     if(dp[x][y]) return dp[x][y];
16     int maxx=1;
17     for(int i=0;i<4;i++){
18         int xx=x+fx[i][0];
19         int yy=y+fx[i][1];
20         if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&arr[xx][yy]>arr[x][y]){
21             maxx=max(maxx,dfs(xx,yy)+1);    //自底向上的
22         }
23     }
24     dp[x][y]=maxx; //记忆化
25     return maxx;
26 }
27 
28 int main(){
29     ios::sync_with_stdio(false);
30     cin>>n>>m;
31     for(int i=1;i<=n;i++)
32         for(int j=1;j<=m;j++)
33         cin>>arr[i][j];
34     int res=0;
35     for(int i=1;i<=n;i++)
36     for(int j=1;j<=m;j++){
37         dp[i][j]=dfs(i,j);
38         res=max(res,dp[i][j]);
39     }
40     cout << res << endl;
41     return 0;
42 }
View Code

猜你喜欢

转载自www.cnblogs.com/qq-1585047819/p/11800897.html