Advertisement
Georgi_Benchev

Untitled

Oct 9th, 2024
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_2;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class Task3_Navigation {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int rowSize = Integer.parseInt(scanner.nextLine());
  12.         int colSize = Integer.parseInt(scanner.nextLine());
  13.         int numberOfMoves = Integer.parseInt(scanner.nextLine());
  14.         int[] code = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  15.         int coeff = Math.max(rowSize, colSize);
  16.  
  17.         BigInteger[][] matrixField = new BigInteger[rowSize][colSize];
  18.         matrixField[rowSize - 1][0] = BigInteger.valueOf(1);
  19.  
  20.         fillingMatrix(rowSize, colSize, matrixField);
  21.  
  22.  
  23.         BigInteger sum = BigInteger.ZERO;
  24.         int currentRow = rowSize - 1;
  25.         int currentCol = 0;
  26.  
  27.         for (int i = 0; i < numberOfMoves; i++) {
  28.             int targetRow = code[i] / coeff;
  29.             int targetCol = code[i] % coeff;
  30.             if (currentCol < targetCol) {
  31.                 for (int horizont = currentCol; horizont <= targetCol; horizont++) {
  32.                     sum = sum.add(matrixField[currentRow][horizont]);
  33.                     matrixField[currentRow][horizont] = BigInteger.valueOf(0);
  34.                 }
  35.                 currentCol = targetCol;
  36.             } else if (currentCol > targetCol) {
  37.  
  38.                 for (int horizont = currentCol; horizont >= targetCol; horizont--) {
  39.                     sum = sum.add(matrixField[currentRow][horizont]);
  40.                     matrixField[currentRow][horizont] = BigInteger.valueOf(0);
  41.  
  42.                 }
  43.                 currentCol = targetCol;
  44.             }
  45.             if (currentRow < targetRow) {
  46.                 for (int vertical = currentRow; vertical <= targetRow; vertical++) {
  47.                     sum = sum.add(matrixField[vertical][currentCol]);
  48.                     matrixField[vertical][currentCol] = BigInteger.valueOf(0);
  49.                 }
  50.                 currentRow = targetRow;
  51.             } else if (currentRow > targetRow) {
  52.  
  53.                 for (int vertical = currentRow; vertical >= targetRow; vertical--) {
  54.                     sum = sum.add(matrixField[vertical][currentCol]);
  55.                     matrixField[vertical][currentCol] = BigInteger.valueOf(0);
  56.                 }
  57.                 currentRow = targetRow;
  58.             }
  59.         }
  60.  
  61.  
  62. //        // better visualisation
  63. //        for (BigInteger[] line : matrixField) {
  64. //            for (BigInteger num : line) {
  65. //                System.out.print(num + " ");
  66. //            }
  67. //            System.out.println();
  68. //        }
  69.  
  70.         System.out.println(sum);
  71.     }
  72.  
  73.     private static void fillingMatrix(int rowSize, int colSize, BigInteger[][] matrixField) {
  74.         for (int row = rowSize - 1; row >= 0; row--) {
  75.             for (int col = 0; col < colSize; col++) {
  76.                 boolean check = true;
  77.                 if (row == rowSize - 1 && col == 0) {
  78.                     check = false;
  79.                 }
  80.                 if (row < rowSize - 1 && check) {
  81.                     matrixField[row][col] = matrixField[row + 1][col].multiply(BigInteger.valueOf(2));
  82.                 } else if (check){
  83.                     matrixField[row][col] = matrixField[row][col - 1].multiply(BigInteger.valueOf(2));
  84.                 }
  85.  
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92. //100
  93. //75
  94. //4
  95. //14 27 1 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement