题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2845
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200000+5;
const int INF=0x3f3f3f3f;
int rowd[maxn],cold[maxn];
//感觉类似数塔,但是数塔只需要知道前一层的状态就好了
//然而 这个需要知道之前所有状态
//那么便是怎么保存之前的状态,空间也不够啊,
//想来想去还是DFS感觉
//然而 看了题解 ,是先预处理一下每行最大值然后DP,因为是隔行算的,直接DP ,woc...
int main()
{
int M,N,n;
while(scanf("%d%d",&M,&N)!=EOF)
{
memset(rowd,0,sizeof(rowd));
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
scanf("%d",&n);
rowd[j]=n;
for(int k=0;k<j-1;k++)
rowd[j]=max(rowd[j],rowd[k]+n);
}
cold[i]=max(rowd[N-1],rowd[N-2]);
int Max=-INF;
for(int j=0;j<i-1;j++)
Max=max(Max,cold[j]);
cold[i]+=Max==-INF?0:Max;
}
cout<<max(cold[M-1],cold[M-2])<<endl;
}
return 0;
}