http://wikioi.com/problem/1169/
四维数组和三维数组;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip> using namespace std;
int num[ 55 ][ 55 ] ;
int dp[ 55 ][ 55 ][ 55 ][ 55 ] ; int main()
{
int n , m ;
while( scanf( "%d%d" , &n , &m ) != EOF )
{
memset( dp , 0 , sizeof( dp ) ) ;
for( int i = 1 ; i <= n ; ++i )
for( int j = 1 ; j <= m ; ++j )
scanf( "%d" , &num[ i ][ j ] ) ;
for( int i1 = 1 ; i1 <= n ; ++i1 )
for( int j1 = 1 ; j1 <= m ; ++j1 )
for( int i2 = 1 ; i2 <= n ; ++i2 )
for( int j2 = 1 ; j2 <= m ; ++j2 )
{
int temp = max( max( dp[ i1 - 1 ][ j1 ][ i2 -1 ][ j2 ] , dp[ i1 - 1][ j1 ][ i2 ][ j2 - 1] ) , max( dp[ i1 ][ j1 -1 ][ i2 -1 ][ j2 ] , dp[ i1 ][ j1 - 1 ][ i2 ][ j2 - 1 ])) ;
if( i1 == i2 && j1 == j2 )
dp[ i1 ][ j1 ][ i2 ][ j2 ] = num[ i1 ][ j1 ] + temp ;
else
dp[ i1 ][ j1 ][ i2 ][ j2 ] = num[ i1 ][ j1 ] + num[ i2 ][ j2 ] + temp ;
}
printf( "%d\n" , dp[ n ][ m ][ n ][ m ] ) ;
}
return 0 ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip> using namespace std; int m , n , mapp[ 51 ][ 51 ] , f[ 101 ][ 51 ][ 51 ] ; void dp()
{
int i , j , k , s , t , Max = 0 ;
for( i = 1 ; i <= m + n - 1 ; ++i )
{
t = ( i > m ) ? m : i ;
s = ( i > n ) ? n : i ;
for( j = i - s + 1 ; j <= t ; ++j )
{
for( k = i - s + 1 ; k <= t ; ++k )
if( j != k || i == m + n - 1 )
{
Max = 0 ;
if( Max < f[ i - 1 ][ j ][ k ] )
Max = f[ i - 1 ][ j ][ k ] ;
if( Max < f[ i - 1 ][ j - 1 ][ k ] )
Max = f[ i - 1 ][ j - 1 ][ k ] ;
if( Max < f[ i - 1 ][ j - 1 ][ k - 1 ] )
Max = f[ i - 1 ][ j - 1 ][ k - 1 ] ;
if( Max < f[ i - 1 ][ j ][ k - 1 ] )
Max = f[ i - 1 ][ j ][ k - 1] ;
f[ i ][ j ][ k ] = Max + mapp[ j ][ i - j + 1 ] + mapp[ k ][ i - k + 1 ] ;
}
} }
}
int main()
{
while( cin >> m >> n )
{
int i , j ;
for( i = 1 ; i <= m ; ++i )
for( j = 1 ; j <= n ; ++j )
cin >> mapp[ i ][ j ] ;
dp() ;
cout << f[ m + n - 1 ][ m ][ m ] << endl ;
}
return 0 ;
}