Ultraviolet a 10827-Maximum sum on a torus

Source: Internet
Author: User
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 most
The 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.