/*结构体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 ) ;
}
05_和最大子矩阵
最新推荐文章于 2024-01-26 11:53:15 发布