Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Test3 {
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- int n = Integer.parseInt(reader.nextLine());
- String[] matrixRow = reader.nextLine().split(" ");
- String[][] matrix = new String[n][matrixRow.length];
- //fill firstRow
- for (int i = 0; i <matrixRow.length ; i++) {
- matrix[0][i] = matrixRow[i];
- }
- //fill rest
- for (int i = 1; i < n; i++) {
- matrixRow = reader.nextLine().split(" ");
- for (int j = 0; j <matrixRow.length ; j++) {
- matrix[i][j] = matrixRow[j];
- }
- }
- int row = 0;
- int col = 0;
- int sum =0;
- int sumMax=Integer.MIN_VALUE;
- String[] commands = reader.nextLine().split(" ");
- for (int i = 0; i <commands.length ; i+=2) {
- row = Integer.parseInt(commands[i]);
- col = Integer.parseInt(commands[i+1]);
- sum = 0;
- if(row>0&&col>0){
- //right & up
- for (int j = 0; j <col ; j++) {
- sum += Integer.parseInt(matrix[row-1][j]);
- }
- for (int j = row-2; j >=0 ; j--) {
- sum+= Integer.parseInt(matrix[j][col-1]);
- }
- }else if(row<0&&col>0){
- //left & up
- for (int j = matrix[(Math.abs(row)-1)].length-1; j >=col-1 ; j--) {
- sum+= Integer.parseInt(matrix[(Math.abs(row)-1)][j]);
- }
- for (int j = (Math.abs(row)-2); j >=0 ; j--) {
- sum+= Integer.parseInt(matrix[j][col-1]);
- }
- }else if(row>0&&col<0){
- //right & down
- for (int j = 0; j <Math.abs(col) ; j++) {
- sum += Integer.parseInt(matrix[row-1][j]);
- }
- for (int j = row; j <matrix.length ; j++) {
- sum += Integer.parseInt(matrix[j][(Math.abs(col)-1)]);
- }
- }else if(row<0&&col<0){
- //left & down
- for (int j = matrix[(Math.abs(row)-1)].length-1; j >=Math.abs(col)-1 ; j--) {
- sum+= Integer.parseInt(matrix[(Math.abs(row)-1)][j]);
- }
- for (int j = Math.abs(row); j <matrix.length ; j++) {
- sum += Integer.parseInt(matrix[j][(Math.abs(col))-1]);
- }
- }
- if(sum > sumMax){
- sumMax = sum;
- }
- }
- System.out.println(sumMax);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment