动态规划--记忆化搜索

https://www.acwing.com/problem/content/903/

记忆化搜索经典题目,滑雪。

只能从高的地方往低的地方滑,问最多能滑多少个点。

 如果这个方程用循环来写的话,听说会非常非常麻烦(反正我不会)

所以就有了记忆化搜索。

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=310;
 5 int g[N][N],f[N][N];
 6 int n,m;
 7 int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
 8 int dfs(int r,int c){
 9     if(f[r][c]!=-1) return f[r][c];
10     f[r][c]=1;
11     for(int i=0;i<4;i++){
12         int tr=r+dx[i],tc=c+dy[i];
13         if(tr>=0&&tr<n&&tc>=0&&tc<=m&&g[tr][tc]<g[r][c]){
14             f[r][c]=max(dfs(tr,tc)+1,f[r][c]);
15         }
16     }
17     return f[r][c];
18 }
19 int main(void){
20     cin>>n>>m;
21     for(int i=0;i<n;i++){
22         for(int j=0;j<m;j++){
23             cin>>g[i][j];
24         }
25     }
26     memset(f,-1,sizeof f);
27     int res=0;
28     for(int i=0;i<n;i++){
29         for(int j=0;j<m;j++){
30             res=max(dfs(i,j),res);
31         }
32     }
33     cout<<res<<endl;
34     return 0;
35 }
原文地址:https://www.cnblogs.com/greenofyu/p/14574808.html