您的位置:首页 > 其它

HDU ACM 1081 最大子矩阵问题

2014-11-13 16:42 267 查看
子矩阵肯定是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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  最大子矩阵