子矩陣肯定是1行,2行,……,n行的。
求出行數爲1的最大子矩陣的值,行數爲2的最大子矩陣的值,……,行數爲n時的最大子矩陣的值。
保存在數組sub_max中, 然後sub_max中的最大值就是最大子序列。
#include<iostream>
using namespace std;
const int M = 105;
int table[M][M] = {0};
int arry[M]= {0};
int sub_max[M];
inline int max_(const int &a,const int &b)
{
return a>b?a:b;
}
int max_subsquence(const int &n_)
{
int res,tmp_;
res = tmp_ = arry[0];
for(int z = 1 ; z < n_ ; z++)
{
tmp_ = max_(arry[z],tmp_+arry[z]);
if(tmp_>res)res = tmp_;
}
return res;
}
int main()
{
int n,tmp,i,j,row,result,tmp_submax;
while(cin >> n)
{
for(i = 0 ; i < n ; i ++)
{
sub_max[i] = -500;
for(j = 0 ; j < n ; j++)cin>>table[i][j];
}
for(row = 1 ; row <= n ; row++)
for(i = row-1 ; i < n ; i++)
{
// cout<<endl << i <<":";
for(j = 0 ; j < n ; j++)
{
tmp = i;
for(int x = 0; x < row ; x++)
{
arry[j] += table[tmp--][j];
}
// cout << arry[j] << " ";
}
tmp_submax = max_subsquence(n);
if(tmp_submax > sub_max[row-1])sub_max[row-1] = tmp_submax;
for(int y = 0 ; y <n ; y++)arry[y] = 0;
}
result = sub_max[0];
for(i = 1; i <n ; i++)result = max_(result,sub_max[i]);
cout <<result<<endl;
}
return 0;
}