This question is an enhanced version for finding the largest submatrix and the original matrix is a matrix ring that can be collusion between the top, bottom, and left. The original method is to copy the original matrix into four, then, the n * n matrix is used as the prototype to copy the mostThe large sub-matrix and the DP, but its complexity is O (n5). After the sub-matrix is handed in, TLE changes several to WA or TLE. Finally, based on the final report, after copying the matrix,
Enumerate the largest child matrix in the upper left corner of the first matrix, and then use the dynamic programming method to find the largest child matrix with a length and width not greater than N. The greatest sum in various enumeration conditions is the solution.
The Code is as follows:
#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<ctime>using namespace std;int a[200][200],b[200],c[200];int main(){#ifdef test freopen("in.txt", "r", stdin);#endif int i,j,t,n,x,y,ans; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0; i<n; i++) for(j=0; j<n; j++) { scanf("%d",&a[i][j]); a[i+n][j] = a[i][j+n] = a[i+n][j+n] = a[i][j]; } ans = -0x7FFFFFFF; for(x=0; x<n; x++) for(y=0; y<n; y++) for(j=0; j<n; j++) for(i=0; i<n; i++) { c[i]=a[i+x][j+y]; if(i) c[i] += c[i-1]; if(j) b[i] += c[i]; else b[i] = c[i]; if(b[i] > ans) ans = b[i]; } printf("%d\n",ans); } return 0;}