一本通 1280:【例9.24】滑雪

滑雪

记忆化搜索


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//Mystery_Sky
//
#define M 500
#define INF 0x3f3f3f3f
int f[M][M];
int r, c, map[M][M], ans;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int dfs(int x, int y)
{
    if(f[x][y]) return f[x][y];
    int maxx = 1;
    int step = 0;
    for(int i = 0; i < 4; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        step = 0;
        if(xx <= 0 || xx > r || yy <= 0 || yy > c) continue;
        if(map[xx][yy] < map[x][y]) step = dfs(xx, yy) + 1;
        maxx = max(maxx, step);
    }
    f[x][y] = maxx;
    return f[x][y];
}

int main() {
    scanf("%d%d", &r, &c);
    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= c; j++) scanf("%d", &map[i][j]);
    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= c; j++) {
            f[i][j] = dfs(i, j);
            ans = max(ans, f[i][j]);
        }
    printf("%d\n", ans);
    return 0;
}
上一篇:20190710记录2:插值图像拼接融合记录


下一篇:洛谷1280 尼克的任务