Design ideas:
First, the value of the integer in the two-dimensional array is found, and then find each positive integer up or down plus a positive negative number. After deciding whether to connect, the small negative number is excluded, and finally left is a two-dimensional integer array of the largest Unicom sub-array.
Program code:
1#include <iostream>2#include <time.h>3 #defineM 34 #defineN 55 using namespacestd;6 7 voidMain ()8 {9 intA[m][n] = {0},b[m][n]={0};//determine connectivity, 0 is unchecked, 1 is selected, and 2 is connectedTen BOOLFLG =0;//determine if there is a 1 presence that exists for O. One intsum =0;//Last and A -Srand (Unsigned (int) Time (0))); - for(inti =0; I < m;i++) the { - for(intj =0; J < n;j++) - { -A[I][J] = rand ()% -- -; +cout << A[i][j] <<"\ t"; - if(A[i][j] >=0) + { AB[I][J] =1; at } - } -cout <<Endl; - } -cout <<Endl; - in for(inti =0; I < m;i++) - { to for(intj =0; J < n;j++) + { - if(B[i][j] = =1) the { * if(a[i+1][J] + a[i][j] >0&& b[i+1][J] = =0) $ {Panax Notoginsengb[i+1][J] =2; - } the if(a[i-1][J] + a[i][j] >0&& b[i-1][J] = =0) + { Ab[i-1][J] =2; the } + if(a[i][j-1] + A[i][j] >0&& b[i][j-1] ==0) - { $b[i][j-1] =2; $ } - if(a[i][j+1] + A[i][j] >0&& b[i][j+1] ==0) - { theb[i][j+1] =2; - } Wuyi } the } - } Wu - for(inti =0; I < m;i++) About { $ for(intj =0; J < n;j++) - { -FLG =0; - if(B[i][j]! =0&& A[i][j] <0) A { +B[I][J] =0; the for(intK =0; k < m;k++) - { $ for(intL =0; l < n;l++) the { the if(B[k][l]! =0) the { the if((b[k+1][L] <=0|| b[k+1][L] >2) && -(b[k-1][L] <=0|| b[k-1][L] >2) && in(b[k][l+1] <=0|| b[k][l+1] >2) && the(b[k][l-1] <=0|| b[k][l-1] >2)) the { AboutFLG =1; the } the } the } + } - if(FLG) the {BayiB[I][J] =2; the } the } - } - } the the for(inti =0; I < m;i++) the { the for(intj =0; J < n;j++) - { the if(B[i][j]! =0) the { thecout << A[i][j] <<"\ t";94Sum + =A[i][j]; the } the Else the {98cout <<"**"<<"\ t"; About } - }101cout <<Endl;102 }103 104cout <<"sum ="<< sum <<Endl; the}
Results:
Summarize:
It's hard when you start thinking, and when you're programming it's true. The result of the implementation is not ideal. Although most of the results will be, but there is still a part of the bug, it is in the future to fill the loopholes, modifications, but the more fill the more, feel should not be so. This is the only way.
Returns the and of the largest interconnected subarray in a two-dimensional integer array