p1296

   一道少见的二维dp。

  由于后效性就很难搞。

  题解中说的是

  然后代码能力差的我写了很长时间也没弄出来。

  但是我难道不能写一个四重循环嘛?50^4也不超时啊,虽然确实没有三重循环优秀吧。

  那么可以推出状态转移方程:

ans[xi][yi][xf][yf]=maxx(ans[xi-1][yi][xf-1][yf],ans[xi-1][yi][xf][yf-1],ans[xi][yi-1][xf-1][yf],ans[xi][yi-1][xf][yf-1])+o[xi][yi]+o[xf][yf];

 

  (maxx是自己写的一个四数取最大值的函数)

  那么AK代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<set>
using namespace std; 
int ans[60][60][60][60],o[60][60];
int n,m;
int i,f;
int xi,yi,xf,yf;
inline int maxx(int a,int b,int c,int d)
{
    return max(max(a,b),max(c,d));
}
int main()
{    
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen("123.in","r",stdin);
    cin>>n>>m;
    for(i=1;i<=n;i++)
        for(f=1;f<=m;f++)
            cin>>o[i][f];
    for(xi=2;xi<=n;xi++)
        for(yi=1;yi<m;yi++)
            for(xf=1;xf<xi;xf++)
                for(yf=2;yf<=m;yf++)
                    ans[xi][yi][xf][yf]=maxx(ans[xi-1][yi][xf-1][yf],ans[xi-1][yi][xf][yf-1],ans[xi][yi-1][xf-1][yf],ans[xi][yi-1][xf][yf-1])+o[xi][yi]+o[xf][yf];
    cout<<ans[n][m-1][n-1][m];
}

 

posted @ 2018-08-06 12:54  zzuqy  阅读(115)  评论(0编辑  收藏  举报