Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class MatrixMaxSum {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. int n = Integer.parseInt(scanner.nextLine());
  10. int[][] matrix = new int[n][];
  11. for (int i = 0; i < n; i++) {
  12. String[] numbers = scanner.nextLine().split("\\s");
  13. matrix[i] = new int[numbers.length];
  14. for (int j = 0; j < numbers.length; j++) {
  15. matrix[i][j] = Integer.parseInt(numbers[j]);
  16. }
  17. }
  18.  
  19. ArrayList<Integer> allSum = new ArrayList<>();
  20. String[] tokens = scanner.nextLine().split("\\s+");
  21.  
  22. for (int i = 0; i < tokens.length - 1; i += 2) {
  23. int col = Integer.parseInt(tokens[i]);
  24. int row = Integer.parseInt(tokens[i + 1]);
  25.  
  26. int sum = 0;
  27. //col+
  28. if (col > 0) {
  29. col--;
  30. //row+
  31. if (row > 0) {
  32. row--;
  33. for (int currCol = 0; currCol <= col; currCol++) {
  34. sum += matrix[currCol][row];
  35. }
  36. }
  37. //row-
  38. else {
  39. row++;
  40. row *= -1;
  41. for (int currCol = col; currCol < matrix.length; currCol++) {
  42. sum += matrix[currCol][row];
  43. }
  44. }
  45. for (int currRow = 0; currRow < row; currRow++) {
  46. sum += matrix[col][currRow];
  47. }
  48. }
  49. //col-
  50. else {
  51. col++;
  52. col *= -1;
  53. //row+
  54. if (row > 0) {
  55. row--;
  56. for (int currCol = col; currCol >= 0; currCol--) {
  57. sum += matrix[currCol][row];
  58. }
  59. }
  60. //row-
  61. else {
  62. row++;
  63. row *= -1;
  64. for (int currCol = col; currCol < matrix.length; currCol++) {
  65. sum += matrix[currCol][row];
  66. }
  67. }
  68. for (int currRow = row + 1; currRow < matrix[0].length; currRow++) {
  69. sum += matrix[col][currRow];
  70. }
  71. }
  72.  
  73. allSum.add(sum);
  74. }
  75.  
  76. Collections.sort(allSum);
  77. System.out.println(allSum.get(allSum.size() - 1));
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement