Advertisement
Georgi_Benchev

Untitled

Oct 9th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_2;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Task4_Bounce {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.  
  11.         int[] parameters = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  12.         int rows = parameters[0];
  13.         int cols = parameters[1];
  14.  
  15.         long[][] matrix = new long[rows][cols];
  16.         long sum = 0;
  17.         fillingTheMatrix(rows, cols, matrix);
  18.  
  19.  
  20.         String diagonalDirection = "RightDown";
  21.  
  22.         int currentRow = 0;
  23.         int currentCol = 0;
  24.  
  25.         while (Math.min(rows, cols) > 1) {
  26.  
  27.             if (diagonalDirection.equals("RightDown")) {
  28.                 if (currentRow < rows - 1 && currentCol < cols - 1) {
  29.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  30.                     currentRow++;
  31.                     currentCol++;
  32.                 } else if (currentRow == rows - 1 && currentCol < cols - 1) {
  33.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  34.                     currentRow--;
  35.                     currentCol++;
  36.                     diagonalDirection = "RightUp";
  37.                 } else if (currentRow < rows - 1 && currentCol == cols - 1) {
  38.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  39.                     currentRow++;
  40.                     currentCol--;
  41.                     diagonalDirection = "LeftDown";
  42.                 }
  43.  
  44.  
  45.             } else if (diagonalDirection.equals("RightUp")) {
  46.                 if (currentRow > 0 && currentCol < cols - 1) {
  47.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  48.                     currentRow--;
  49.                     currentCol++;
  50.                 } else if (currentRow == 0 && currentCol < cols - 1) {
  51.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  52.                     currentRow++;
  53.                     currentCol++;
  54.                     diagonalDirection = "RightDown";
  55.                 } else if (currentRow > 0 && currentCol == cols - 1) {
  56.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  57.                     currentRow--;
  58.                     currentCol--;
  59.                     diagonalDirection = "LeftUp";
  60.                 }
  61.             } else if (diagonalDirection.equals("LeftDown")) {
  62.                 if (currentRow < rows - 1 && currentCol > 0) {
  63.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  64.                     currentRow++;
  65.                     currentCol--;
  66.                 } else if (currentRow == rows - 1 && currentCol > 0) {
  67.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  68.                     currentRow--;
  69.                     currentCol--;
  70.                     diagonalDirection = "LeftUp";
  71.                 } else if (currentRow < rows - 1 && currentCol == 0) {
  72.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  73.                     currentRow++;
  74.                     currentCol++;
  75.                     diagonalDirection = "RightDown";
  76.                 }
  77.             } else if (diagonalDirection.equals("LeftUp")) {
  78.                 if (currentRow > 0 && currentCol > 0) {
  79.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  80.                     currentRow--;
  81.                     currentCol--;
  82.                 } else if (currentRow == 0 && currentCol > 0) {
  83.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  84.                     currentRow++;
  85.                     currentCol--;
  86.                     diagonalDirection = "LeftDown";
  87.                 } else if (currentRow > 0 && currentCol == 0) {
  88.                     sum = getSum(sum, currentCol, matrix[currentRow]);
  89.                     currentRow--;
  90.                     currentCol++;
  91.                     diagonalDirection = "RightUp";
  92.                 }
  93.             }
  94.  
  95.             if ((currentRow == 0 && currentCol == 0)
  96.                     || (currentRow == 0 && currentCol == cols - 1)
  97.                     || (currentRow == rows - 1 && currentCol == 0)
  98.                     || (currentRow == rows - 1 && currentCol == cols - 1)) {
  99.                 break;
  100.             }
  101.         }
  102.         sum = getSum(sum, currentCol, matrix[currentRow]);
  103.  
  104.         System.out.println(sum);
  105.     }
  106.  
  107.     private static long getSum(long sum, int currentCol, long[] matrix) {
  108.         sum += matrix[currentCol];
  109.         return sum;
  110.     }
  111.  
  112.     private static void fillingTheMatrix(int rows, int cols, long[][] matrix) {
  113.         for (int row = 0; row < rows; row++) {
  114.             for (int col = 0; col < cols; col++) {
  115.                 matrix[row][col] = (long) Math.pow(2, row + col);
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement