Advertisement
butoff

Bounce

Nov 24th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Bounce {
  5.  
  6.     private static String[] directions = {"DR", "DL", "UR", "UL"};
  7.     private static String direction = directions[0];
  8.     private static Scanner scanner = new Scanner(System.in);
  9.     private static long[][] matrix;
  10.     private static int row = -1;
  11.     private static int col = -1;
  12.     private static long sum = 0;
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.         int r = scanner.nextInt();
  17.         int c = scanner.nextInt();
  18.  
  19.         matrix = new long[r][c];
  20.  
  21.         if (r == 1 || c == 1){
  22.             sum = 1;
  23.             Exit();
  24.         }
  25.  
  26.         initMatrix();
  27.  
  28.         while (true) {
  29.  
  30.             proceed();
  31.  
  32.         }
  33.  
  34.     }
  35.  
  36.     private static void proceed() {
  37.  
  38.         boolean success = true;
  39.  
  40.         switch (direction) {
  41.             case "DR":
  42.                 success = moveDownRight();
  43.                 break;
  44.             case "DL":
  45.                 success = moveDownLeft();
  46.                 break;
  47.             case "UL":
  48.                 success = moveUpLeft();
  49.                 break;
  50.             case "UR":
  51.                 success = moveUpRight();
  52.                 break;
  53.         }
  54.         if (success){
  55.             sum += matrix[row][col];
  56.         }
  57.     }
  58.  
  59.     private static boolean moveDownRight() {
  60.         if (invalidDown() && invalidRight()) {
  61.             Exit();
  62.         } else if (invalidDown()) {
  63.             direction = directions[2];
  64.             return false;
  65.         } else if (invalidRight()) {
  66.             direction = directions[1];
  67.             return false;
  68.         }
  69.         row++;
  70.         col++;
  71.         direction = directions[0];
  72.         return true;
  73.     }
  74.  
  75.     private static boolean moveDownLeft() {
  76.         if (invalidDown() && invalidLeft()) {
  77.             Exit();
  78.         } else if (invalidDown()) {
  79.             direction = directions[3];
  80.             return false;
  81.         } else if (invalidLeft()) {
  82.             direction = directions[0];
  83.             return false;
  84.         }
  85.         row++;
  86.         col--;
  87.         direction = directions[1];
  88.         return true;
  89.     }
  90.  
  91.     private static boolean moveUpRight() {
  92.         if (invalidUp() && invalidRight()) {
  93.             Exit();
  94.         } else if (invalidUp()) {
  95.             direction = directions[0];
  96.             return false;
  97.         } else if (invalidRight()) {
  98.             direction = directions[3];
  99.             return false;
  100.         }
  101.         row--;
  102.         col++;
  103.         direction = directions[2];
  104.         return true;
  105.     }
  106.  
  107.     private static boolean moveUpLeft() {
  108.         if (invalidUp() && invalidLeft()) {
  109.             Exit();
  110.         } else if (invalidUp()) {
  111.             direction = directions[1];
  112.             return false;
  113.         } else if (invalidLeft()) {
  114.             direction = directions[2];
  115.             return false;
  116.         }
  117.         row--;
  118.         col--;
  119.         direction = directions[3];
  120.         return true;
  121.     }
  122.  
  123.     private static boolean invalidDown() {
  124.         return row + 1 >= matrix.length;
  125.     }
  126.  
  127.     private static boolean invalidUp() {
  128.         return row - 1 < 0;
  129.     }
  130.  
  131.     private static boolean invalidRight() {
  132.         return col + 1 >= matrix[0].length;
  133.     }
  134.  
  135.     private static boolean invalidLeft() {
  136.         return col - 1 < 0;
  137.     }
  138.  
  139.     private static void Exit(){
  140.         System.out.println(sum);
  141.         System.exit(0);
  142.     }
  143.  
  144.     private static void initMatrix() {
  145.  
  146.         int firstInRow = 1;
  147.         for (int r = 0; r < matrix.length; r++) {
  148.  
  149.             matrix[r][0] = firstInRow;
  150.             firstInRow *= 2;
  151.  
  152.             for (int c = 1; c < matrix[r].length; c++) {
  153.                 matrix[r][c] = 2 * matrix[r][c - 1];
  154.             }
  155.         }
  156.     }
  157.  
  158.     private static void printMatrix() {
  159.  
  160.         for (int i = 0; i < matrix.length; i++) {
  161.             System.out.println(Arrays.toString(matrix[i]));
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement