The largest interconnected subarray of two-dimensional arrays and

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.