Maximum Connectivity sub-array

Source: Internet
Author: User

Topic:

Returns the and of the largest interconnected subarray in a two-dimensional integer array.

Requirements:

1. Enter a two-dimensional shaping array with positive numbers in the array and negative values. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

2. The array to be used by the program is placed in a file called Input.txt, the file format is: The number of rows of the array, the number of columns of the array, the elements of each line, (separated by commas) each number is a signed 32-bit integer, of course, the number of rows and columns is a positive integer.

Design ideas:

The number in the matrix is randomly generated, there may be a positive negative, so to first determine which positions in the two-dimensional array are positive numbers, the other two-dimensional array to record the position of positive numbers, and then determine which number is connected, my idea is to first determine whether the two number of rows (number of columns) is the same, if the same, Then determine whether the number of columns (number of rows) is adjacent, if the adjacent is to prove two number of connections, calculate the number of each connected block and then connect the different blocks, calculate its and, and finally compare the maximum value, that is, a two-dimensional integer array of the largest Unicom sub-array.

SOURCE program code:

#include <iostream>#include<ctime>using namespacestd;#defineM 100#include<fstream>intMax (intNintA[],int*SM,int*mm) {    intb[ -] = {0 }; intI, Ssum =0, mmax=0;  for(i =0; i<n; i++)    {        if(ssum<0) {ssum=A[i]; }        Else{ssum= Ssum +A[i]; } B[i]=ssum; } Mmax= b[0];  for(i =0; i<n; i++)    {        if(mmax<B[i]) {Mmax=B[i]; *MM =i; }    }     for(i = *mm; I >=0; i--)    {        if(B[i] = =A[i]) {            *SM =i;  Break; }    }    returnMmax;}voidMain () {ofstream outfile; Outfile.open ("a.txt"); if(!outfile) {Cerr<<"OPEN error!"<<Endl; Exit (0);    } srand ((_int32) time (NULL)); intm, N, I, J, SM, MM, max1; intsum, Max; intLine[m], list[m], t[m]; intStr[m][m], b[m]; cout<<"rows for two-dimensional arrays:"; CIN>>m; OutFile<<m<<Endl; cout<<"columns for two-dimensional arrays:"; CIN>>N; OutFile<<n<<Endl;  for(i =0; I <m; i++)    {         for(j =0; j<n; J + +) {Str[i][j]= rand ()%Ten; if(rand ()%2==1) Str[i][j]= str[i][j]* (-1); cout<<STR[i][j]<<" "; OutFile<<STR[i][j]<<" "; } cout<<Endl; OutFile<<Endl; }     for(i =0; i<m; i++)    {         for(j =0; j<n; J + +) {B[j]=Str[i][j]; } Sum= Max (n, B, &AMP;SM, &mm); Line[i]=SM; List[i]=mm; T[i]=sum; } max1= t[0];  for(i =0; i +1<m; i++)    {        if(Line[i] <= list[i +1] && List[i] >= line[i +1]) {max1=max1+ T[i +1]; }         for(j = line[i]; J<line[i +1]; J + +)        {            if(Str[i +1][j]>0) Max1=max1+ Str[i +1][j]; }} cout<<"The sum of the largest interconnected sub-arrays"<< Max1 <<Endl; OutFile<<max1;}

Operation Result:

Group members: Yang Tao http://www.cnblogs.com/GloryYT/

Maximum Connectivity sub-array

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.