UVA 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp记忆化搜索)

题意:在一个R*C(R, C<=100)的整数矩阵上找一条高度严格递减的最长路。起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩阵外。矩阵中的数均为0~100。

分析:dp[x][y]为从位置(x,y)出发的最长路。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int dp[MAXN][MAXN];
int pic[MAXN][MAXN];
int r, c;
bool judge(int x, int y){
    return x >= 0 && x < r && y >= 0 && y < c;
}
int dfs(int x, int y){
    if(dp[x][y] != -1) return dp[x][y];
    int ans = 0;
    for(int i = 0; i < 4; ++i){
        int tmpx = x + dr[i];
        int tmpy = y + dc[i];
        if(judge(tmpx, tmpy) && pic[tmpx][tmpy] < pic[x][y]){
            ans = Max(ans, dfs(tmpx, tmpy));
        }
    }
    return dp[x][y] = ans + 1;
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        string name;
        cin >> name;
        scanf("%d%d", &r, &c);
        for(int i = 0; i < r; ++i){
            for(int j = 0; j < c; ++j){
                scanf("%d", &pic[i][j]);
            }
        }
        memset(dp, -1, sizeof dp);
        int ans = 0;
        for(int i = 0; i < r; ++i){
            for(int j = 0; j < c; ++j){
                ans = Max(ans, dfs(i, j));
            }
        }
        printf("%s: %d\n", name.c_str(), ans);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6426559.html