【HDOJ】2571 命运
DP。
1 /* 2571 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 25 7 #define MAXM 1005 8 #define INF -999999 9 10 int map[MAXN][MAXM]; 11 int dp[MAXN][MAXM]; 12 13 int max(int a, int b) { 14 return a>b ? a:b; 15 } 16 17 int main() { 18 int t, n, m; 19 int i, j, k; 20 21 #ifndef ONLINE_JUDGE 22 freopen("data.in", "r", stdin); 23 #endif 24 25 scanf("%d", &t); 26 while (t--) { 27 scanf("%d %d", &n, &m); 28 for (i=1; i<=n; ++i) 29 for (j=1; j<=m; ++j) 30 scanf("%d", &map[i][j]); 31 32 for (j=0; j<=m; ++j) 33 dp[0][j] = INF; 34 for (i=0; i<=n; ++i) 35 dp[i][0] = INF; 36 dp[0][1] = dp[1][0] = 0; 37 for (i=1; i<=n; ++i) { 38 for (j=1; j<=m; ++j) { 39 dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 40 k = 1; 41 while (j >= k) { 42 if (j%k == 0) 43 dp[i][j] = max(dp[i][j], dp[i][j/k]); 44 ++k; 45 } 46 dp[i][j] += map[i][j]; 47 } 48 } 49 printf("%d\n", dp[n][m]); 50 } 51 52 return 0; 53 }