标签:int sizeof can scan space pre continue map main
刚开始的时候错在了第一行第一列的转移。
注意:第一行第一列上的数据只能由该行或者该列转移,因为他是从(1,1)这个点出发的,不是任意的点出发的。
所以除了dp[1][0],dp[0][1],其他dp值都要初始化为-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;
}
标签:int sizeof can scan space pre continue map main
原文地址:https://www.cnblogs.com/tennant/p/8976654.html