HDU ACM 1081 最大子矩阵问题

本文介绍了一种求解不同行数时的最大子矩阵序列的算法,通过遍历矩阵并计算不同行数子矩阵的最大元素序列,最终找出全局最大序列。

子矩阵肯定是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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值