HDU 2571 [simple dp]

At the beginning, the mistake was in the transfer of the first row and first column.
Note: The data on the first row and the first column can only be transferred from this row or this column, because it starts from the point (1, 1), not any point.
So except dp[1][0], dp[0][1], other dp values ​​should be initialized to -INF

#include<stdio.h>
#include<string.h>
#include<iostream>
#define MAX 1000+10
#define INF 0x1f1f1f1f
using namespace std;
int dp[30][MAX];
int n, m;
int map[30][MAX];
int max(int a, int b) {
    return (a > b) ? a : b;
}


int main(void) {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                scanf("%d", &map[i][j]);
            }

        }
        memset(dp, -INF, sizeof(dp));

        dp[1][0] = dp[0][1] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = max(dp[i][j - 1] + map[i][j], dp[i][j]);
                dp[i][j] = max(dp[i][j], dp[i - 1][j] + map[i][j]);
                for (int k = 1; k < j; k++) {
                    if (j%k != 0)
                        continue;

                    dp[i][j] = max(dp[i][j], dp[i][k] + map[i][j]);

                }
            }
        }
        printf("%d\n", dp[n][m]);

    }

    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325122149&siteId=291194637