Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.Scanner;
- public class navigation2 {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int R = in.nextInt();
- int C = in.nextInt();
- int N = in.nextInt();
- BigInteger start = BigInteger.valueOf(1);
- BigInteger currentCell = start;
- BigInteger[][] matrix = new BigInteger[R][C];
- for (int row = R - 1; row >= 0 ; row--) {
- for (int col = 0; col < C; col++) {
- matrix[row][col] = currentCell;
- currentCell = currentCell.multiply(BigInteger.valueOf(2));
- // System.out.printf("%d ",matrix[row][col]);
- }
- // System.out.println();
- start = start.multiply(BigInteger.valueOf(2));
- currentCell = start;
- }
- int coef = Math.max(R, C);
- BigInteger sum = BigInteger.ONE;
- int currentRow = R - 1;
- int currentCol = 0;
- boolean[][] wasHere = new boolean[R][C];
- wasHere[currentRow][currentCol] = true;
- in.nextLine();
- String[] codes = in.nextLine().split(" ");
- for (int moves = 0; moves < codes.length ; moves++) {
- int codeRow = Integer.parseInt(codes[moves]) / coef;
- int codeCol = Integer.parseInt(codes[moves]) % coef;
- if (currentCol < codeCol) {
- for (int i = currentCol; i < codeCol; i++) {
- currentCol++;
- if (wasHere[currentRow][currentCol] == false) {
- sum = sum.add(matrix[currentRow][currentCol]);
- wasHere[currentRow][currentCol] = true;
- }
- }
- } else {
- for (int i = currentCol; i > codeCol; i--) {
- currentCol--;
- if (wasHere[currentRow][currentCol] == false) {
- sum = sum.add(matrix[currentRow][currentCol]);
- wasHere[currentRow][currentCol] = true;
- }
- }
- }
- if (currentRow < codeRow) {
- for (int i = currentRow; i < codeRow; i++) {
- currentRow++;
- if (wasHere[currentRow][currentCol] == false) {
- sum = sum.add(matrix[currentRow][currentCol]);
- wasHere[currentRow][currentCol] = true;
- }
- }
- } else {
- for (int i = currentRow; i > codeRow; i--) {
- currentRow--;
- if (wasHere[currentRow][currentCol] == false) {
- sum = sum.add(matrix[currentRow][currentCol]);
- wasHere[currentRow][currentCol] = true;
- }
- }
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement