To find the maximum number of interconnected sub-arrays in a two-dimensional integer array

Source: Internet
Author: User

1 problem: Returns the number of the largest interconnected subarray in a two-dimensional integer array

2 idea: To decompose the two-dimensional array of n*m, decomposed into n one-dimensional arrays, and then the largest subarray of n one-dimensional arrays and note the subscript of the largest one-dimensional sub-array of each row, such as 2-5, which is divided into two cases the first is the largest sub-array between rows is connected, such as the first row is 2-5, the second row is 3-6, This is directly added to the line. The second is not connected, such as the first row is 2-5, the second row is 6-7, this time the largest sub-array of each row as a whole, and then make each of the largest array of blocks connected, so that the minimum cost of its connection. Finally, we can find out the maximum number of interconnected sub-arrays.

3 Code

1#include <iostream>2 using namespacestd;3 intZuida (intNintA[],int*SM,int*mm);4 5 voidMain ()6 {7     intm,n,i,j,sm,mm,t2;8     intSum,max;9     intup[ -],down[ -],t[ -];Ten     inta[ -][ -],b[ -]; Onecout<<"Enter rows for two-dimensional arrays"<<Endl; ACin>>m; -cout<<"Enter rows for two-dimensional arrays"<<Endl; -Cin>>N; the      for(i=0; i<m;i++) -     { -          for(j=0; j<n;j++) -         { +Cin>>A[i][j]; -         } +     } A   at      for(i=0; i<m;i++) -     { -          for(j=0; j<n;j++) -         { -b[j]=A[i][j]; -         } inSum=zuida (n,b,&sm,&mm); -up[i]=SMA;  todown[i]=mm; +t[i]=sum; -   the     } *t2=t[0]; $      for(i=0; i+1<m;i++)Panax Notoginseng     { -         if(up[i]<=down[i+1] && down[i]>=up[i+1]) the         { +t2+=t[i+1]; A         } the           for(j=up[i];j<up[i+1];j++) +         { -             if(a[i+1][j]>0) t2+=a[i+1][J];//distinguishing independent positive numbers $         } $   -     } -cout<<t2<<Endl; the   - }Wuyi  the  - intZuida (intNintA[],int*SM,int*mm) Wu { -     intb[ -]={0}; About     inti,sum1=0, max1=0; $      for(i=0; i<n;i++) -     { -         if(sum1<0) -         { Asum1=A[i]; +         } the         Else -         { $sum1=sum1+A[i]; the         } theb[i]=sum1; the     } themax1=b[0]; -      for(i=0; i<n;i++) in     { the         if(max1<B[i]) the          { Aboutmax1=B[i]; the*MM =i; the          } the     } +       for(i = *mm;i >=0; i--) -     { the         if(B[i] = =A[i])Bayi         { the*sm=i; the               Break; -         } -     } the      returnmax1; the } the  

4

5 Summary: I feel this topic is more difficult than the last topic, the difficulty is to make each row the largest sub-array connected, because not directly connected to get the maximum value, but also to judge the size, like the data structure in the shortest path problem, using the Dijkstra algorithm. Program is always a combination of different modules, programming to achieve a step-by-step implementation of each function module, is a complex procedure simplification, one by break.

To find the maximum number of interconnected sub-arrays in a two-dimensional integer array

Related Article

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.