Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- public class MatrixMaxSum {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int[][] matrix = new int[n][];
- for (int i = 0; i < n; i++) {
- String[] numbers = scanner.nextLine().split("\\s");
- matrix[i] = new int[numbers.length];
- for (int j = 0; j < numbers.length; j++) {
- matrix[i][j] = Integer.parseInt(numbers[j]);
- }
- }
- ArrayList<Integer> allSum = new ArrayList<>();
- String[] tokens = scanner.nextLine().split("\\s+");
- for (int i = 0; i < tokens.length - 1; i += 2) {
- int col = Integer.parseInt(tokens[i]);
- int row = Integer.parseInt(tokens[i + 1]);
- int sum = 0;
- //col+
- if (col > 0) {
- col--;
- //row+
- if (row > 0) {
- row--;
- for (int currCol = 0; currCol <= col; currCol++) {
- sum += matrix[currCol][row];
- }
- }
- //row-
- else {
- row++;
- row *= -1;
- for (int currCol = col; currCol < matrix.length; currCol++) {
- sum += matrix[currCol][row];
- }
- }
- for (int currRow = 0; currRow < row; currRow++) {
- sum += matrix[col][currRow];
- }
- }
- //col-
- else {
- col++;
- col *= -1;
- //row+
- if (row > 0) {
- row--;
- for (int currCol = col; currCol >= 0; currCol--) {
- sum += matrix[currCol][row];
- }
- }
- //row-
- else {
- row++;
- row *= -1;
- for (int currCol = col; currCol < matrix.length; currCol++) {
- sum += matrix[currCol][row];
- }
- }
- for (int currRow = row + 1; currRow < matrix[0].length; currRow++) {
- sum += matrix[col][currRow];
- }
- }
- allSum.add(sum);
- }
- Collections.sort(allSum);
- System.out.println(allSum.get(allSum.size() - 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement