其他分享
首页 > 其他分享> > [AcWing 901] 滑雪

[AcWing 901] 滑雪

作者:互联网

image
image

记忆化搜索


点击查看代码
#include<iostream>
#include<cstring>

using namespace std;

const int N = 310;
int n, m;
int h[N][N];
int f[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int dp(int x, int y)
{
    int &v = f[x][y];
    if (v != -1)    return v;
    v = 1;
    for (int i = 0; i < 4; i ++) {
        int a = x + dx[i], b = y + dy[i];
        if (a > 0 && a <= n && b > 0 && b <= m && h[a][b] < h[x][y])
            v = max(v, dp(a, b) + 1);
    }
    return v;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            cin >> h[i][j];
    memset(f, -1, sizeof f);
    int res = 0;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            res = max(res, dp(i, j));
    cout << res << endl;
    return 0;
}

  1. 状态表示
    \(f[i][j]\) 表示所有从 \((i,j)\) 开始滑的路径的最大值
  2. 状态转移
    枚举 \(f[i][j]\) 的上下左右四个方向,需要枚举的条件:没有越过边界,高度小于 \(h[i][j]\)
    \(f[i][j] = 1 + max(f[i-1][j],f[i+1][j],f[i][j-1],f[i][j+1])\)

标签:901,&&,滑雪,int,枚举,dx,dy,include,AcWing
来源: https://www.cnblogs.com/wKingYu/p/16340682.html