Title Requirements:
Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
The maximum value for the and of all sub-arrays.
Design ideas:
The two-dimensional array is considered as the form of the graph, and then the Ergodic method is taken.
When and less than 0 are discarded, the other overlays and.
Code:
#include <iostream>#include<ctime>using namespacestd;#defineN 100typedefstruct{ intDian[n]; intXian[n][n]; intDianx, Xianx;} A;void Set(A &shu,intXinty) {Shu.dianx= x*y; Srand ((unsigned) time (NULL)); for(inti =1; I <= Shu.dianx; i++) {Shu.dian[i]= rand ()%Ten; if(rand ()%2==1) Shu.dian[i]= Shu.dian[i] * (-1); } for(inti =1; I <= Shu.dianx; i + =y) { for(intj = i; J <= i + Y-2; J + +) {shu.xian[j][j+1] =1; Shu.xian[j+1][J] =1; } } for(inti =1+ y; i<shu.dianx; i + =y) { for(intj = i; J <= i + X-1; J + +) {shu.xian[j][j-Y] =1; Shu.xian[j-Y][J] =1; } }}voidoutput (A shu) { for(inti =1; I <= Shu.dianx; i++) {cout<<Shu.dian[i]; if(Shu.xian[i][i +1] ==1) cout<<" "; Elsecout<<Endl; }}voidBianli (A &shu,intVintVisit[],int&b,int&max,intx) {Visit[v]=1; Max+=Shu.dian[v]; if(Max >=b) B=Max; intA =0, Bo =0; for(intW =1; W <= Shu.dianx; w++) { for(intc =1; C <= Shu.dianx; C++) { if((visit[w] = =0) && (shu.xian[c][w] = =1) && (visit[c] = =1) ) {a= W; Bo =1; Break; } } if(Bo = =1) Break; } for(intW =1; W <= Shu.dianx; w++) { for(intc =1; C <= Shu.dianx; C++) { if((visit[w] = =0) && (shu.xian[c][w] = =1) && (visit[c] = =1)) { if(shu.dian[a]<Shu.dian[w]) a=W; } } } if(b + shu.dian[a]<0) {Shu.xian[v][a]=0; } ElseBianli (Shu, a, visit, B, max, x);}intNovisit (intvisit[], A Shu) { intK =0, I; for(i =1; I <= Shu.dianx; i++) { if(Visit[i] = =0) {k=i; Break; } } returnK;}intMain () {cout<<"Please enter the number of array rows:"<<Endl; intx, y; CIN>> x >>y; A Shu; Set(Shu, x, y); Output (SHU); intv =1, b[n] = {0}, H =0; for(inti =1; I <= Shu.dianx; i++) { if(shu.dian[i]<0) {B[i]=Shu.dian[i]; } Else { intVisit[n] = {0 }; intMax =0; Bianli (Shu, I, visit, B[i], Max, x); } } intmax = b[1]; for(inti =2; I <= Shu.dianx; i++) { if(b[i]>max) Max=B[i]; } cout<<"the same as the maximum number of interconnected sub-arrays:"<< Max <<Endl;}
View Code
:
Summarize:
Although there are several previous questions about the array of bedding and progressive, but this problem is really very difficult. There are not many searches available.
The idea is very important, the thought determines the ability to parse the program to write.
Maximum Connectivity sub-array