Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class Navigation {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int r = Integer.parseInt(scan.nextLine()); // rows
- int c = Integer.parseInt(scan.nextLine()); //coloms
- int movies = Integer.parseInt(scan.nextLine());
- double[] code = new double[movies];
- for (int i = 0; i < movies; i++) {
- code[i] = scan.nextDouble();
- }
- if(r == 1 && c == 1){
- System.out.println(1);
- return;
- }
- int coff = Integer.max(r, c);
- boolean[][] matrix = new boolean[r][c];
- BigInteger sum =BigInteger.ZERO;
- BigInteger power = BigInteger.TWO;
- int pawnRow = r - 1;
- int pawnCol = 0;
- BigInteger rowPowerOfTwo;
- BigInteger colPowerofTwo;
- for (int t = 0; t < movies; t++) {
- int targetRow = (int) (code[t]) / coff;
- int targetCol = (int) (code[t]) % coff;
- if (pawnCol <= targetCol && pawnRow <= targetRow) {
- for (int col = pawnCol; col <= targetCol; col++) {
- if (matrix[pawnRow][col]){
- continue;
- }
- rowPowerOfTwo = power.pow(r - 1 - pawnRow);
- colPowerofTwo = power.pow(col);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[pawnRow][col] = true;
- }
- pawnCol = targetCol;
- for (int row = pawnRow; row <= targetRow; row++) {
- if (matrix[row][pawnCol]){
- continue;
- }
- rowPowerOfTwo = power.pow(r - 1 - row);
- colPowerofTwo = power.pow(pawnCol);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[row][pawnCol] =true;
- }
- pawnRow = targetRow;
- } else if (pawnCol <= targetCol && pawnRow > targetRow) {
- for (int col = pawnCol; col <= targetCol; col++) {
- if (matrix[pawnRow][col]){
- continue;
- }
- rowPowerOfTwo = power.pow(r - 1 - pawnRow);
- colPowerofTwo = power.pow(col);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[pawnRow][col] = true;
- }
- pawnCol = targetCol;
- for (int row = targetRow; row <= pawnRow; row++) {
- if (matrix[row][pawnCol]){
- continue;
- }
- rowPowerOfTwo = power.pow( r - 1 - row );
- colPowerofTwo = power.pow(pawnCol);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[row][pawnCol] = true;
- }
- pawnRow = targetRow;
- } else if (pawnCol > targetCol && pawnRow <= targetRow) {
- for (int col = targetCol; col <= pawnCol; col++) {
- if (matrix[pawnRow][col]){
- continue;
- }
- rowPowerOfTwo = power.pow(r- 1 - pawnRow);
- colPowerofTwo = power.pow(col);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[pawnRow][col] = true;
- }
- pawnCol = targetCol;
- for (int row = pawnRow; row <= targetRow; row++) {
- if (matrix[row][pawnCol]){
- continue;
- }
- rowPowerOfTwo = power.pow(r - 1 - row);
- colPowerofTwo = power.pow(pawnCol);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[row][pawnCol] = true;
- }
- pawnRow = targetRow;
- } else {
- for (int col = targetCol; col <= pawnCol; col++) {
- if (matrix[pawnRow][col]){
- continue;
- }
- rowPowerOfTwo = power.pow(r - 1 - pawnRow);
- colPowerofTwo = power.pow(col);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[pawnRow][col] = true;
- }
- pawnCol = targetCol;
- for (int row = targetRow; row <= pawnRow; row++) {
- if (matrix[row][pawnCol]){
- continue;
- }
- rowPowerOfTwo = power.pow( r - 1 -row);
- colPowerofTwo = power.pow(pawnCol);
- sum = sum.add(rowPowerOfTwo.multiply(colPowerofTwo));
- matrix[row][pawnCol] = true;
- }
- pawnRow = targetRow;
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement