SlavCodes

Untitled

Sep 8th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import java.util.*;
  2. public class Test3 {
  3.  
  4.  
  5. public static void main(String[] args) {
  6. Scanner reader = new Scanner(System.in);
  7. int n = Integer.parseInt(reader.nextLine());
  8. String[] matrixRow = reader.nextLine().split(" ");
  9. String[][] matrix = new String[n][matrixRow.length];
  10. //fill firstRow
  11. for (int i = 0; i <matrixRow.length ; i++) {
  12. matrix[0][i] = matrixRow[i];
  13. }
  14. //fill rest
  15. for (int i = 1; i < n; i++) {
  16. matrixRow = reader.nextLine().split(" ");
  17. for (int j = 0; j <matrixRow.length ; j++) {
  18. matrix[i][j] = matrixRow[j];
  19. }
  20. }
  21. int row = 0;
  22. int col = 0;
  23. int sum =0;
  24. int sumMax=Integer.MIN_VALUE;
  25. String[] commands = reader.nextLine().split(" ");
  26. for (int i = 0; i <commands.length ; i+=2) {
  27. row = Integer.parseInt(commands[i]);
  28. col = Integer.parseInt(commands[i+1]);
  29. sum = 0;
  30. if(row>0&&col>0){
  31. //right & up
  32. for (int j = 0; j <col ; j++) {
  33. sum += Integer.parseInt(matrix[row-1][j]);
  34. }
  35. for (int j = row-2; j >=0 ; j--) {
  36. sum+= Integer.parseInt(matrix[j][col-1]);
  37. }
  38. }else if(row<0&&col>0){
  39. //left & up
  40. for (int j = matrix[(Math.abs(row)-1)].length-1; j >=col-1 ; j--) {
  41. sum+= Integer.parseInt(matrix[(Math.abs(row)-1)][j]);
  42. }
  43. for (int j = (Math.abs(row)-2); j >=0 ; j--) {
  44. sum+= Integer.parseInt(matrix[j][col-1]);
  45. }
  46. }else if(row>0&&col<0){
  47. //right & down
  48. for (int j = 0; j <Math.abs(col) ; j++) {
  49. sum += Integer.parseInt(matrix[row-1][j]);
  50. }
  51. for (int j = row; j <matrix.length ; j++) {
  52. sum += Integer.parseInt(matrix[j][(Math.abs(col)-1)]);
  53. }
  54. }else if(row<0&&col<0){
  55. //left & down
  56. for (int j = matrix[(Math.abs(row)-1)].length-1; j >=Math.abs(col)-1 ; j--) {
  57. sum+= Integer.parseInt(matrix[(Math.abs(row)-1)][j]);
  58. }
  59. for (int j = Math.abs(row); j <matrix.length ; j++) {
  60. sum += Integer.parseInt(matrix[j][(Math.abs(col))-1]);
  61. }
  62.  
  63. }
  64. if(sum > sumMax){
  65. sumMax = sum;
  66. }
  67. }
  68. System.out.println(sumMax);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment