Vijos 1255 mooncake Box maximum sub-matrix

Source: Internet
Author: User

Test Instructions:Link Method:Maximum sub-matrix parsing:have done so many of the largest sub-matrix, this problem casually yy came out, but I have an idea ah, if a n*m matrix, there are some bad points, non-bad points of the right value, can be negative, then the selection of the maximum and how much of the non-bad idea matrix? It seems that DP? First of all, because the bad point weight is 0, you can preprocess the sum[i,j] for the lower right corner, (I,J) for the upper left corner of the matrix weights and. We can, of course, brute-force enumeration matrices, but this is a 4-time side. Again observed n,m<=1000, so we can choose to calculate ♂ method ② to do this problem. The sum is preprocessed first, and the le,ri,h. Then enumerate all the good points and calculate the four vertex coordinates of their maximal sub-matrices, and a simple allowance is a random one. Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 1010Using namespace Std;int n,m;int map[N][N];bool m[N][N];int sum[N][N];int le[N][N];int ri[N][N];int h[N][N];void Getsum () {for (int i=1;i<=n;i++)    {int s=0;for (int j=1;j<=m;j++)        {S+=map[i][j];Sum[i][j]=s+sum[i-1][j];        }     }}void Init () {for (int i=1;i<=n;i++)    {for (int j=1;j<=m;j++)        {if (! M[I][J])le[i][j]=0;else le[i][j]=le[i][j-1]+1;        }for (int j=m;j>=1;j--)        {if (! M[I][J])ri[i][j]=0;else ri[i][j]=ri[i][j+1]+1;        }    }for (int i=1;i<=n;i++)    {for (int j=1;j<=m;j++)        {if (M[i][j]&&m[i-1][j])            {h[i][j]=h[i-1][j]+1;le[i][j]=min (Le[i][j],le[i-1][j]);ri[i][j]=min (Ri[i][j],ri[i-1][j]);            }        }    }} int main () {scanf ("%d%d", &n,&m);for (int i=1;i<=n;i++)    {for (int j=1;j<=m;j++)        {scanf ("%d", &map[i][j]);m[i][j]=!map[i][j]?0:1;        }    }getsum ();init ();int ans=0;for (int i=1;i<=n;i++)    {for (int j=1;j<=m;j++)        {if (M[i][j])            {int ptx1,pty1,ptx2,pty2,ptx3,pty3,ptx4,pty4;Ptx1=i,pty1=j-le[i][j];Ptx2=i-h[i][j]-1,pty2=j-le[i][j];ptx3=i-h[i][j]-1,pty3=j+ri[i][j]-1;ptx4=i,pty4=j+ri[i][j]-1;Ans=max (Ans,sum[ptx4][pty4]-sum[ptx1][pty1]-sum[ptx3][pty3]+sum[ptx2][pty2]);            }        }    }printf ("%d\n", ans);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Vijos 1255 mooncake Box maximum sub-matrix

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.