HELP
查看原帖
HELP
247992
_Cloud_楼主2020/10/21 23:02
#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 205;
const int M = 205;

bool vis[N][M];
int map[N][M], f[N][M];
int n, m;

int dp(int x, int y) {//记忆化搜索
    // if (x < 1 || x > n || y < 1 || y > m)
    //     return 0;
    if (x == 1)
        return f[x][y] = map[x][y];
    if (vis[x][y])
        return f[x][y];
    
    int res = -214748;

    res = max(res, dp(x-1, y));//向前
    if (x - 1 >= 1 && y - 1 >= 1)
        res = max(res, dp(x-1, y-1));//左前
    if (x - 1 >= 1 && y + 1 <= m)
        res = max(res, dp(x-1, y+1));//右前
    
    res += map[x][y];
    f[x][y] = res;
    vis[x][y] = true;//赋值

    return res;
}

int main() {
    scanf("%d %d", &n, &m);

    for (int i = 1; i <= n; i++)
        for (int j= 1; j <= m; j++)
            scanf("%d", &map[i][j]); 

    printf("%d\n", dp(n, m / 2 + 1));

    for (int i = 1; i <= n; i++) {//调试
        for (int j = 1; j <= m; j++)
            printf("%d ", f[i][j]);
        printf("\n");
    }
    return 0;
}
2020/10/21 23:02
加载中...