Ideas:
This is also based on the students using the matrix into a diagram of the idea of the results are drawn, so I went along this way step-by-step analysis of a bit. First, the two-dimensional matrix is converted into a storage form of the graph, when the two adjacent numbers are connected, the length of the record is 1, otherwise it is 0; the entire graph begins to traverse through each point, while traversing, when and less than 0 o'clock break the road between two points, when and when the maximum and the maximum and change the value The maximum value of the number of bytes to be traversed at each point as the starting point and the current maximum. When traversing, select the maximum value traversal around the traversed sub-array of Unicom. The maximum number of connected arrays in this array can be determined by repeating several traversal attempts.
Title Requirements:
Put it in a input.txt file.
There are positive numbers in the array.
Returns the Unicom sub-array and the maximum value
#include <fstream>#include<iostream>#defineN 100#include<ctime>using namespaceStd;typedefstruct{ intD[n]; intA[n][n]; intx;} A;void Set(A &p,intXintY//x, y are the number of rows and columns, respectively{p.x= x*y; Srand ((unsigned) time (NULL)); for(inti =1; I <= p.x; i++) {P.d[i]= rand ()%Ten; if(rand ()%2==1) P.d[i]= P.d[i] * (-1); } //the number of randomly generated arrays for(inti =1; I <= p.x; i + =y) { for(intj = i; J <= i + Y-2; J + +) {p.a[j][j+1] =1; P.a[j+1][J] =1; } } for(inti =1+ y; i<p.x; i + =y) { for(intj = i; J <= i + X-1; J + +) {p.a[j][j-Y] =1; P.a[j-Y][J] =1; } } //convert randomly generated one-dimensional arrays into two-dimensional graphs}voidBianli (A &p,intVintVisit[],int&b,int&max,intx) {Visit[v]=1; Max+=P.d[v]; if(Max >=b) B=Max; intA =0, Bo =0; for(intW =1; W <= p.x; w++) { for(intc =1; C <= p.x; C++) { if((visit[w] = =0) && (p.a[c][w] = =1) && (visit[c] = =1) ) {a= W; Bo =1; Break; } } if(Bo = =1) Break; } for(intW =1; W <= p.x; w++) { for(intc =1; C <= p.x; C++) { if((visit[w] = =0) && (p.a[c][w] = =1) && (visit[c] = =1)) { if(p.d[a]<P.d[w]) a=W; } } } if(b + p.d[a]<0) {P.a[v][a]=0; } ElseBianli (P, a, visit, B, max, x);} //TraverseintNovisit (intvisit[], A p) { intK =0, I; for(i =1; I <= p.x; i++) { if(Visit[i] = =0) {k=i; Break; } } returnK;} //judging the item without visit in the graphintMain () {cout<<"Please enter the number of rows and columns of the array:"<<Endl; intx, y; CIN>> x >>y; A p; Set(P, x, y); Ofstream Fout ("D:\\input.txt", ios::binary); for(inti =1; I <= p.x; i++) {Fout<<P.d[i]; if(P.a[i][i +1] ==1) Fout<<" "; ElseFout<<Endl; } intv =1, b[n] = {0}, H =0; for(inti =1; I <= p.x; i++) { if(p.d[i]<0) {B[i]=P.d[i]; } Else { intVisit[n] = {0 }; intMax =0; Bianli (P, I, visit, B[i], Max, x); } } intmax = b[1]; for(inti =2; I <= p.x; i++) { if(b[i]>max) Max=B[i]; } fout<<"the same as the maximum number of interconnected sub-arrays:"<< Max <<Endl;}
A two-dimensional array of interconnected sub-arrays and maximum