题目链接:https://www.luogu.com.cn/problem/P1006
思想同P1004完全相同:
P1004:https://www.luogu.com.cn/problem/P1004
思路:https://www.cnblogs.com/New-ljx/p/13866374.html
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int N=55; 5 int g[N][N],f[N][N][N][N]; 6 int n,m; 7 int main(){ 8 scanf("%d%d",&m,&n); 9 for(int i=1;i<=m;i++) 10 for(int j=1;j<=n;j++) scanf("%d",&g[i][j]); 11 for(int i=1;i<=m;i++) 12 for(int j=1;j<=n;j++) 13 for(int k=1;k<=m;k++){ 14 int q=i+j-k; 15 if(q<=0) continue; 16 f[i][j][k][q]+=max(f[i-1][j][k-1][q],max(f[i-1][j][k][q-1],max(f[i][j-1][k-1][q],f[i][j-1][k][q-1]))); 17 if(i==k&&j==q) f[i][j][k][q]+=g[i][j]; 18 else f[i][j][k][q]+=g[i][j]+g[k][q]; 19 } 20 printf("%d",f[m][n][m][n]); 21 return 0; 22 }