P1434 [SHOI2002]滑雪

题解

比较简单的记忆化搜索,存储在此位置的最长距离,便于其他位置计算。


Code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int n,m;
int cot[101][101];
int max_p[101][101];

int r_m[4] = {1,-1,0,0};
int c_m[4] = {0,0,1,-1};

int dfs(int i, int j){
    if( max_p[i][j] > 0 ) 
        return max_p[i][j];
    int ret,h,l;
    bool down = false;// 可以优化省略 此处保留易于理解
    ret = 0;
    for(int k=0;k<4;k++){
        h = i+r_m[k];
        l = j+c_m[k];

        if( h>=0 && h<n && l>=0 && l<m && cot[h][l] < cot[i][j]){
            ret = max(dfs(h,l)+1,ret);
            down = true;
        }
    }

    if(!down) ret = 1;
    return (max_p[i][j] = ret) ;
}

int main(void){

    cin>>n>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>> cot[i][j];
    int ans = 0;    
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            ans = max(ans, dfs(i,j));

    cout<<ans<<endl;
    return 0;

}

猜你喜欢

转载自blog.csdn.net/smmyy022/article/details/81505846