05_和最大子矩阵

/*结构体Node存放每个找到的子矩阵及其和*/
typedef struct node Node ;
struct node{

    int i1 , i2 , j1 , j2 , sum ;
    Node& operator=( Node &other ){
        this->sum = other.sum ;
        this->i1 = other.i1 ;
        this->i2 = other.i2 ;
        this->j1 = other.j1 ;
        this->j2 = other.j2 ;
        return *this ;
    } 
};
/*在行中抽两个数,在列中抽两个数,这四个数就可以组成一个子矩阵;遍历这个子矩阵,将这个子矩阵中的值相加放入temp中,以此类推得出全部子矩阵*/
#define N 4
void getAll( vector<Node> &all , int arr[][N] , int len ){

    Node temp = { 0,0,0,0,0 } ;
    Node max = {0,0,0,0,0} ;
    for( int i1=0 ; i1<len ; ++i1){
        for( int i2=i1 ; i2<len ; ++i2){
            for( int j1=0 ; j1<len ; ++j1){
                for( int j2=j1 ; j2<len ; ++j2){
                    temp.i1 = i1 ; 
                    temp.i2 = i2 ;
                    temp.j1 = j1 ;
                    temp.j2 = j2 ;
                    for( int i=i1 ; i<=i2 ; ++i ){
                        for( int j=j1 ; j<=j2 ; ++j ){
                            temp.sum += arr[i][j] ;
                        }
                    }
                    all.push_back( temp ) ;
                    if( temp.sum>max.sum ){
                        max = temp ;
                    }
                    temp.i1 = 0 ;
                    temp.i2 = 0 ;
                    temp.j1 = 0 ;
                    temp.j2 = 0 ;
                    temp.sum = 0 ;
                }
            }
        }
    }
    all.push_back( max ) ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值