Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. public class navigation2 {
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.         int R = in.nextInt();
  8.         int C = in.nextInt();
  9.         int N = in.nextInt();
  10.         BigInteger start = BigInteger.valueOf(1);
  11.         BigInteger currentCell = start;
  12.         BigInteger[][] matrix = new BigInteger[R][C];
  13.         for (int row = R - 1; row >= 0 ; row--) {
  14.             for (int col = 0; col < C; col++) {
  15.                 matrix[row][col] = currentCell;
  16.                 currentCell = currentCell.multiply(BigInteger.valueOf(2));
  17. //                System.out.printf("%d ",matrix[row][col]);
  18.             }
  19. //            System.out.println();
  20.             start = start.multiply(BigInteger.valueOf(2));
  21.             currentCell = start;
  22.         }
  23.         int coef = Math.max(R, C);
  24.         BigInteger sum = BigInteger.ONE;
  25.         int currentRow = R - 1;
  26.         int currentCol = 0;
  27.         boolean[][] wasHere = new boolean[R][C];
  28.         wasHere[currentRow][currentCol] = true;
  29.         in.nextLine();
  30.         String[] codes = in.nextLine().split(" ");
  31.         for (int moves = 0; moves < codes.length ; moves++) {
  32.             int codeRow = Integer.parseInt(codes[moves]) / coef;
  33.             int codeCol = Integer.parseInt(codes[moves]) % coef;
  34.             if (currentCol < codeCol) {
  35.                 for (int i = currentCol; i < codeCol; i++) {
  36.                     currentCol++;
  37.                     if (wasHere[currentRow][currentCol] == false) {
  38.                         sum = sum.add(matrix[currentRow][currentCol]);
  39.                         wasHere[currentRow][currentCol] = true;
  40.                     }
  41.                 }
  42.             } else {
  43.                 for (int i = currentCol; i > codeCol; i--) {
  44.                     currentCol--;
  45.                     if (wasHere[currentRow][currentCol] == false) {
  46.                         sum = sum.add(matrix[currentRow][currentCol]);
  47.                         wasHere[currentRow][currentCol] = true;
  48.                     }
  49.                 }
  50.             }
  51.             if (currentRow < codeRow) {
  52.                 for (int i = currentRow; i < codeRow; i++) {
  53.                     currentRow++;
  54.                     if (wasHere[currentRow][currentCol] == false) {
  55.                         sum = sum.add(matrix[currentRow][currentCol]);
  56.                         wasHere[currentRow][currentCol] = true;
  57.                     }
  58.                 }
  59.             } else {
  60.                 for (int i = currentRow; i > codeRow; i--) {
  61.                     currentRow--;
  62.                     if (wasHere[currentRow][currentCol] == false) {
  63.                         sum = sum.add(matrix[currentRow][currentCol]);
  64.                         wasHere[currentRow][currentCol] = true;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         System.out.println(sum);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement