Design topic: Find a two-dimensional array in the connected array and the maximum maximum value.
Design ideas:
The two-dimensional array is built and the two-dimensional array is traversed, and all positive integers are divided into blocks to verify whether they are connected or not, and if not, determine the path.
Package demo;
Import java.util.*;
public class Lmax {
static Scanner Scanner = new Scanner (system.in);
public static void Main (String args[]) {
int m,n;
int b;
Scanner Scanner = new Scanner (system.in);
System.out.println ("Please enter the number of columns of the two-dimensional array:");
m = Scanner.nextint ();
System.out.println ("Please enter the number of rows for the two-dimensional array:");
n = scanner.nextint ();
int arr[][] = new Int[n][m];
System.out.println ("Please enter:");
for (int i = 0;i<n;i++)
for (int j=0;j<m;j++)
{
ARR[I][J] = Scanner.nextint ();
}
System.out.println ("\ n");
b = Maxarrsum (arr);
System.out.println ("Max Unicom array and for" +b);
}
public static int[][] Arrsum (int arr[][]) {
int m = arr.length;
int n = arr[0].length;
int p[][] = new int[m+1][n+1];
P[0][0] = arr[0][0];
for (int i=0; i<=m; i++) p[i][0] = 0;
for (int i=0; i<=n; i++) p[0][i] = 0;
for (int i=1; i<=m; i++) {
for (int j=1; j<=n; J + +) {
P[I][J] = P[i-1][j] + p[i][j-1] + arr[i-1][j-1]-p[i-1][j-1];
}
}
return p;
}
static int maxarrsum (int arr[][]) {
int m = arr.length;
int n = arr[0].length;
int p[][] = arrsum (arr);
int ans = integer.min_value;
for (int i=1; i<=m; i++) {
for (int j=1; j<=n; J + +) {
for (int endi=i; Endi <=m; endi++) {
for (int endj=j; endj<=n; endj++) {
int sum = P[ENDI][ENDJ]-P[I-1][ENDJ]-p[endi][j-1] + p[i-1][j-1];
if (ans < sum) ans = sum;
}
}
}
}
return ans;
}
}
Program:
Summary: The completion of this homework, is my classmates and Zhang Jiaxing work together to find ideas and improve the procedures, the implementation of the algorithm is more complex, in our mutual discussion can be gradually explored, to carry out the simplification of complex problems, and successfully prepared.
The largest interconnected subarray of two-dimensional arrays and