59 lines
1.4 KiB
Java
59 lines
1.4 KiB
Java
import java.util.Scanner;
|
|
|
|
public class HourGlass {
|
|
|
|
/*
|
|
* 1 1 1 0 0 0
|
|
* 0 1 0 0 0 0
|
|
* 1 1 1 0 0 0
|
|
* 0 0 0 0 0 0
|
|
* 0 0 0 0 0 0
|
|
* 0 0 0 0 0 0
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
int[][] ar = matrix();
|
|
System.out.println(sumInt(ar));
|
|
}
|
|
|
|
private static int[][] matrix() {
|
|
Scanner scanner = new Scanner(System.in);
|
|
System.out.println("Please enter the 2D Matrix elements below:");
|
|
System.out.println("Numbers of rows: ");
|
|
int rows = scanner.nextInt();
|
|
System.out.println("Numbers of Columns: ");
|
|
int columns = scanner.nextInt();
|
|
int[][] ar = new int[rows][columns];
|
|
for (int i = 0; i < rows; i++) {
|
|
for (int j = 0; j < columns; j++) {
|
|
ar[i][j] = scanner.nextInt();
|
|
}
|
|
System.out.println();
|
|
}
|
|
for (int i = 0; i < rows; i++) {
|
|
for (int j = 0; j < columns; j++) {
|
|
System.out.print(ar[i][j] + "\t");
|
|
}
|
|
System.out.println();
|
|
}
|
|
scanner.close();
|
|
return ar;
|
|
}
|
|
|
|
private static int sumInt(int[][] arr) {
|
|
int rows = arr.length;
|
|
int columns = arr[0].length;
|
|
|
|
int total = -63;
|
|
for (int i = 0; i < rows - 2; i++) {
|
|
for (int j = 0; j < columns - 2; j++) {
|
|
int currentSum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j]
|
|
+ arr[i + 2][j + 1] + arr[i + 2][j + 2];
|
|
total = Math.max(total, currentSum);
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
|
|
}
|