RT,代码如下
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n , m ;
int Map[1005][1005] ;
long long f[1005] , up[1005] , set[1005];
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
memset(up,0xc0,sizeof(up)) ;
memset(set,0xc0,sizeof(set)) ;
cin >> n >> m ;
for ( int i = 1 ; i <= n ; i ++ ) {
for ( int j = 1 ; j <= m ; j ++ ) {
cin >> Map[i][j] ;
}
}
for ( int i = 1 ; i <= n ; i ++) {
f[i] = f[i-1] + Map[i][1] ;
}
for ( int i = 2 ; i < m ; i ++ ) {
memset(up,0xc0,sizeof(up)) ;
memset(set,0xc0,sizeof(set)) ;
for ( int j = n ; j >= 1 ; j -- ) {
up[j] =max(f[j],up[j+1])+Map[j][i];
}
for ( int j = 1 ; j <= n ; j ++ ) {
set[j] = max (f[j], set[j-1]) + Map[j][i] ;
}
for ( int j = 1 ; j <= n ; j ++ ) {
f[j] = max (up[j],set[j]) ;
}
}
for ( int j = 1 ; j <= n ; j ++ ) {
f[j] = max(f[j],f[j-1])+Map[j][m];
}
cout << f[n] ;
return 0;
}