一本通 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;
}

猜你喜欢

转载自www.cnblogs.com/Benjamin-cpp/p/11025626.html
今日推荐