yavorn

Untitled

Dec 5th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. import java.io.ByteArrayInputStream;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class MatrixMaxSum {
  6.  
  7.     static void fakeInput() {
  8.         String test = "6\n" +
  9.                 "1 2 3 4 5 6\n" +
  10.                 "2 3 4 5 6 7\n" +
  11.                 "6 5 4 3 2 1\n" +
  12.                 "3 4 5 6 7 8\n" +
  13.                 "4 5 6 7 8 9\n" +
  14.                 "9 8 7 6 5 4\n" +
  15.                 "3 5 3 -5 -4 -2";
  16.         System.setIn(new ByteArrayInputStream(test.getBytes()));
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         fakeInput();
  21.         Scanner sc = new Scanner(System.in);
  22.         int n = sc.nextInt();
  23.         sc.nextLine();
  24.         String[][] matrix = new String[n][];
  25.  
  26.         for (int row = 0; row < n; row++) {
  27.             String[] line = sc.nextLine().split(" ");
  28.             matrix[row] = line;
  29.         }
  30.  
  31.         String[] coordinatesInput = sc.nextLine().trim().split(" ");
  32.         int[] nextRow = new int[coordinatesInput.length / 2];
  33.         int[] nextCol = new int[coordinatesInput.length / 2];
  34.         int counter = 0;
  35.         for (int index = 0; index < coordinatesInput.length; index += 2) {
  36.             nextRow[counter] = Integer.parseInt(coordinatesInput[index]);
  37.             nextCol[counter] = Integer.parseInt(coordinatesInput[index + 1]);
  38.             counter++;
  39.         }
  40.  
  41.         int cRow;
  42.         int cCol;
  43.         int tRow;
  44.         int tCol;
  45.         long maxSum = Long.MIN_VALUE;
  46.  
  47.         for (int index = 0; index < nextRow.length; index++) {
  48.             tCol = Math.abs(nextCol[index]) - 1;
  49.             long sum = 0;
  50.  
  51.             if (nextRow[index] >= 0) {
  52.                 cRow = nextRow[index] - 1;
  53.                 cCol = 0;
  54.             } else {
  55.                 cRow = Math.abs(nextRow[index]) - 1;
  56.                 cCol = matrix[0].length - 1;
  57.             }
  58.  
  59.             if (nextCol[index] >= 0) {
  60.                 tRow = 0;
  61.             } else {
  62.                 tRow = matrix.length - 1;
  63.             }
  64.  
  65.             while (cCol != tCol && cCol >= 0 && cCol < matrix[0].length) {
  66.                 sum += Integer.parseInt(matrix[cRow][cCol]);
  67.                 if (tCol > cCol) {
  68.                     cCol++;
  69.                 } else {
  70.                     cCol--;
  71.                 }
  72.                 if (cCol < 0 || cCol >= matrix.length) {
  73.                     break;
  74.                 }
  75.             }
  76.  
  77.             while (cRow != tRow) {
  78.                 sum += Integer.parseInt(matrix[cRow][cCol]);
  79.                 if (tRow > cRow) {
  80.                     cRow++;
  81.                 } else {
  82.                     cRow--;
  83.                 }
  84.                 if (cRow < 0 || cRow >= matrix.length) {
  85.                     break;
  86.                 }
  87.             }
  88.             sum += Integer.parseInt(matrix[cRow][cCol]);
  89.  
  90.             if (sum > maxSum) {
  91.                 maxSum = sum;
  92.             }
  93. //            System.out.println(sum);
  94.         }
  95.         System.out.println(maxSum);
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment