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
#include <iostream>using namespacestd;intZuida (intNintA[],int*SM,int*mm);voidMain () {intm,n,i,j,sm,mm,t2; intSum,max; intup[ -],down[ -],t[ -]; inta[ -][ -],b[ -]; cout<<"Enter rows for two-dimensional arrays"<<Endl; CIN>>m; cout<<"Enter rows for two-dimensional arrays"<<Endl; CIN>>N; for(i=0; i<m;i++) { for(j=0; j<n;j++) {cin>>A[i][j]; } } for(i=0; i<m;i++) { for(j=0; j<n;j++) {B[j]=A[i][j]; } Sum=zuida (n,b,&sm,&mm); Up[i]=SMA; Down[i]=mm; T[i]=sum; } T2=t[0]; for(i=0; i+1<m;i++) { if(up[i]<=down[i+1] && down[i]>=up[i+1]) {T2+=t[i+1]; } 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;}intZuida (intNintA[],int*SM,int*mm) { intb[ -]={0}; inti,sum1=0, max1=0; for(i=0; i<n;i++) { if(sum1<0) {sum1=A[i]; } Else{sum1=sum1+A[i]; } B[i]=sum1; } max1=b[0]; for(i=0; i<n;i++) { if(max1<B[i]) {Max1=B[i]; *MM =i; } } for(i = *mm;i >=0; i--) { if(B[i] = =A[i]) { *sm=i; Break; } } returnmax1;}
4 Summary: This topic is more difficult than the last question
Returns the 6 of the largest interconnected subarray in a two-dimensional integer array.