SHOW:
|
|
- or go back to the newest paste.
| 1 | public static void main(String[] args) throws IOException {
| |
| 2 | BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); | |
| 3 | ||
| 4 | int[][] matrix = readMatrix(rd); | |
| 5 | int[][] bestM = new int[3][3]; | |
| 6 | int max = 0; | |
| 7 | ||
| 8 | for (int i = 0; i < matrix.length - 2; i++) {
| |
| 9 | for (int j = 0; j < matrix[i].length - 2; j++) {
| |
| 10 | ||
| 11 | int[][] mat = getNewMatrix(matrix, i, j); | |
| 12 | int sum = getSum(mat); | |
| 13 | ||
| 14 | if (sum > max) {
| |
| 15 | max = sum; | |
| 16 | bestM = mat; | |
| 17 | } | |
| 18 | } | |
| 19 | } | |
| 20 | ||
| 21 | System.out.println("Sum = " + max);
| |
| 22 | printMatrix(bestM); | |
| 23 | } | |
| 24 | ||
| 25 | private static int getSum(int[][] mat) {
| |
| 26 | int sum = 0; | |
| 27 | for (int[] ints : mat) | |
| 28 | sum += Arrays.stream(ints).sum(); | |
| 29 | return sum; | |
| 30 | } | |
| 31 | ||
| 32 | private static int[][] getNewMatrix(int[][] matrix, int i, int j) {
| |
| 33 | int[][] mat = new int[3][3]; | |
| 34 | for (int k = 0; k < 3; k++) | |
| 35 | mat[k] = Arrays.copyOfRange(matrix[i + k], j, j + 3); | |
| 36 | return mat; | |
| 37 | } | |
| 38 | ||
| 39 | private static void printMatrix(int[][] ar) {
| |
| 40 | for (int[] nums : ar) | |
| 41 | System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
| |
| 42 | } | |
| 43 | ||
| 44 | private static int[][] readMatrix(BufferedReader rd) throws IOException {
| |
| 45 | int[] size = Arrays.stream(rd.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
| |
| 46 | int[][] mat = new int[size[0]][size[1]]; | |
| 47 | for (int i = 0; i < mat.length; i++) | |
| 48 | mat[i] = Arrays.stream(rd.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
| |
| 49 | return mat; | |
| 50 | } |