#include<iostream>
using namespace std;
//给定一个矩阵a(0...m-1,0...n-1),求出它的一个子矩阵b(i1...i2,j1...j2),使得b的所有元素和最大
void MaxSum(int **a,int m,int n,int &besti1,int &besti2,int &bestj1,int &bestj2,int &sum)
{
int b;
int i1,i2,j1,j2;
int i,j;
for(i2=m-1;i2>=0;i2--)
{
for(j2=n-1;j2>=0;j2--)
{
for(i1=0;i1<=i2;i1++)
{
for(j1=0;j1<=j2;j1++)
{
b=0;
for(i=i1;i<=i2;i++)
{
for(j=j1;j<=j2;j++)
{
b+=a[i][j];
}
}
if(b>sum)
{
sum=b;
besti1=i1;
besti2=i2;
bestj1=j1;
bestj2=j2;
}
}
}
}
}
}
void main()
{
int m,n;
cout<<"输入矩阵的行:";
cin>>m;
cout<<"输入矩阵的列:";
cin>>n;
int **a=new int*[n];
int i,j;
for(i=0;i<m;i++)
{
a[i]=new int[n];
}
for(i=0;i<m;i++)
{
cout<<"输入第"<<i<<"行数据:"<<endl;
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
int i1,i2,j1,j2,sum=0;
MaxSum(a,m,n,i1,i2,j1,j2,sum);
cout<<"最大矩阵和的矩阵是:"<<endl;
for(i=i1;i<=i2;i++)
{
for(j=j1;j<=j2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"最大矩阵和是:"<<sum<<endl;
}