PackageShengcheng;ImportJava.util.Scanner;Importorg.junit.Test; Public classErweixunhuan {@Test Public voidFun () {int[][]a=New int[4] [8]; Scanner Write=NewScanner (system.in); for(inti=0;i<4;i++) { for(intj=0;j<4;j++) {A[i][j]=Write.nextint (); A[i][j+4]=A[i][j]; } } intmax=-200000; int[]sum=New int[8]; intAdd =-200000 ; for(inti=0;i<4;i++) { for(intl=0;l<8;l++) {Sum[l]=0; } for(intj=i;j<4;j++) { for(intk=0;k<8;k++) {Sum[k]+=A[j][k]; } Add=DP (sum,4); } if(add>max) {Max=add; }} System.out.println (max); } Public intDP (intA[],intN) {intMax=a[0]; inttemp=-200000; for(inti=0;i<n;i++) {Temp=-200000; for(intj=i;j<n+i;j++) {Temp+=A[j]; if(temp>Max) {Max=temp; } if(temp<0) {Temp=A[j]; } } } returnMax; } }
Teacher out of this topic, I first think of the original one-dimensional array loop to find the largest sub-matrix, and then think of the original two-dimensional array to find the maximum sub-matrix, feel can use the original idea to continue thinking, so I thought the original two-dimensional array of the largest sub-matrix is also the two-dimensional conversion to one-dimensional And then the one-dimensional array of dynamic planning, so that the original two-dimensional problem, so I used the same method to expand the number of two-dimensional column as a result of the cycle, and then the two-dimensional array into a one-dimensional array, and then this can be used on the original one-dimensional loop array to solve the problem.
A two-dimensional array loop to find the largest sub-array