P1006 传纸条
普通的四维dp
(当然有一些很ok的方法然鹅我比较菜)
$f[i][j][k][u]$表示第一张纸条在$(i,j)$,第二张在$(k,u)$
复杂度$O(n^{4})$
#include<iostream> #include<cstdio> #include<cstring> #define re register using namespace std; int max(int &a,int &b) {return a>b ?a:b;} int n,m,a[52][52],f[52][52][52][52]; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) scanf("%d",&a[i][j]); f[1][2][2][1]=a[1][2]+a[2][1]; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) for(int k=1;k<=n;++k) for(int u=1;u<=m;++u){ if(i==k&&j==u) continue;//不能一个人传2张 f[i+1][j][k+1][u]=max(f[i+1][j][k+1][u],f[i][j][k][u]+a[i+1][j]+a[k+1][u]); f[i+1][j][k][u+1]=max(f[i+1][j][k][u+1],f[i][j][k][u]+a[i+1][j]+a[k][u+1]); f[i][j+1][k+1][u]=max(f[i][j+1][k+1][u],f[i][j][k][u]+a[i][j+1]+a[k+1][u]); f[i][j+1][k][u+1]=max(f[i][j+1][k][u+1],f[i][j][k][u]+a[i][j+1]+a[k][u+1]); } printf("%d",max(f[n-1][m][n][m-1],f[n][m-1][n-1][m])); return 0; }