Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2499 Accepted Submission(s): 893
1 3 8 9 10 10 10 10 -10 10 10 10 -11 -1 0 2 11 10 -20 -11 -11 10 11 2 10 -10 -10
52
看到仙剑人物!不过还没玩完仙剑4。这题就是简单DP,就是数塔的变形版而已,基于数塔的题目,只要加些条件,状态转移方程很容易找到,这样就行了!
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571
代码:
#include <iostream> #include <stdio.h> #include <memory.h> using namespace std; int a[25][1005], sum[25][1005]; int main() { int i, j, k, t, n, m, MAX; scanf("%d", &t); while(t--) { scanf("%d %d", &n ,&m); for(i = 0; i <= n; i++) for(j = 0; j <= m; j++) a[i][j] = -999; for(i = 1; i <= n; i++) for(j = 1; j <= m; j++) scanf("%d", &a[i][j]); for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) { MAX = -999; for(k = 1; k < j; k++) { if(j%k == 0) if(a[i][k] > MAX) MAX = a[i][k]; } if(i-1 >= 1 && j-1 >= 1) a[i][j] += max( max(a[i-1][j], a[i][j-1]), MAX ); else if(i-1 >= 1) a[i][j] += max( a[i-1][j], MAX ); else if(j - 1 >= 1) a[i][j] += max( a[i][j-1], MAX ); } } printf("%d\n", a[n][m]); } return 0; }