四维飘过
C++ 代码
#include<bits/stdc++.h>
using namespace std;
int f[55][55][55][55],n,m,a[55][55];
#define max(x,y) ((x)>(y)?(x):(y))
#define max4(x,y,w,z) max(max(x,y),max(w,z))
template<typename T>inline void read(T &x) {
x=0;
T f=1,ch=getchar();
while (!isdigit(ch)) {if(ch=='-') f=-1; ch=getchar();}
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
x*=f;
}
int main() {
read(n); read(m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
read(a[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
for(int k=1;k<=n;k++)
for(int l=1;l<=m;l++) {
f[i][j][k][l]=max4(f[i-1][j][k-1][l],f[i-1][j][k][l-1],f[i][j-1][k][l-1],f[i][j-1][k-1][l])+a[k][l]+a[i][j];
if(i==k||j==l) f[i][j][k][l]-=a[i][j];
}
cout<<f[n][m][n][m]<<endl;
return 0;
}
三维飘过
#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 55
using namespace std;
int f[2*maxn][maxn][maxn];
int a[maxn][maxn];
int n,m;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int k=1;k<=n+m-1;k++)
for(int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
if(k-i+1<1||k-j+1<1) continue;
f[k][i][j]=max(max(f[k-1][i][j],f[k-1][i-1][j-1]),max(f[k-1][i][j-1],f[k-1][i-1][j]))+a[i][k-i+1]+a[j][k-j+1];
if(i==j) f[k][i][j]-=a[i][k-i+1];
}
cout<<f[n+m-1][n][n]<<endl;
return 0;
}