跟8年前NOIP 2000 方格取数的题目几乎一样,不多解释了,代码:
#include <stdio.h>
#include <stdlib.h>
#define min(a, b) ((a)<(b)?(a):(b))
#define max(a, b) ((a)>(b)?(a):(b))
int map[51][51];
int f[100][51][51];
int main(int argc, char **argv)
{
int i, j, k, c;
int m, n;
scanf("%d%d", &m, &n);
for(i = 1; i <= m; i++){
for(j = 1; j <= n; j++){
scanf("%d", &map[i][j]);
}
}
for(i = 2; i < m + n - 1; i++){
for(j = 1; j <= min(m, i); j++){
for(k = 1; k <= min(m, i); k++){
if(j == k){
continue;
}
c = map[j][i - j + 1] + map[k][i - k + 1];
f[i][j][k] = max(f[i][j][k], f[i - 1][j - 1][k - 1] + c);
f[i][j][k] = max(f[i][j][k], f[i - 1][j][k - 1] + c);
f[i][j][k] = max(f[i][j][k], f[i - 1][j - 1][k] + c);
f[i][j][k] = max(f[i][j][k], f[i - 1][j][k] + c);
}
}
}
printf("%d\n", f[m + n - 2][m - 1][m]);
return 0;
}