码迷,mamicode.com
首页 > 其他好文 > 详细

p1296

时间:2018-08-06 13:46:05      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:lib   cin   algorithm   stack   std   max   分享图片   cstring   info   

   一道少见的二维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];
}

 

p1296

标签:lib   cin   algorithm   stack   std   max   分享图片   cstring   info   

原文地址:https://www.cnblogs.com/qywyt/p/9429658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!