Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Bounce {
- private static String[] directions = {"DR", "DL", "UR", "UL"};
- private static String direction = directions[0];
- private static Scanner scanner = new Scanner(System.in);
- private static long[][] matrix;
- private static int row = -1;
- private static int col = -1;
- private static long sum = 0;
- public static void main(String[] args) {
- int r = scanner.nextInt();
- int c = scanner.nextInt();
- matrix = new long[r][c];
- if (r == 1 || c == 1){
- sum = 1;
- Exit();
- }
- initMatrix();
- while (true) {
- proceed();
- }
- }
- private static void proceed() {
- boolean success = true;
- switch (direction) {
- case "DR":
- success = moveDownRight();
- break;
- case "DL":
- success = moveDownLeft();
- break;
- case "UL":
- success = moveUpLeft();
- break;
- case "UR":
- success = moveUpRight();
- break;
- }
- if (success){
- sum += matrix[row][col];
- }
- }
- private static boolean moveDownRight() {
- if (invalidDown() && invalidRight()) {
- Exit();
- } else if (invalidDown()) {
- direction = directions[2];
- return false;
- } else if (invalidRight()) {
- direction = directions[1];
- return false;
- }
- row++;
- col++;
- direction = directions[0];
- return true;
- }
- private static boolean moveDownLeft() {
- if (invalidDown() && invalidLeft()) {
- Exit();
- } else if (invalidDown()) {
- direction = directions[3];
- return false;
- } else if (invalidLeft()) {
- direction = directions[0];
- return false;
- }
- row++;
- col--;
- direction = directions[1];
- return true;
- }
- private static boolean moveUpRight() {
- if (invalidUp() && invalidRight()) {
- Exit();
- } else if (invalidUp()) {
- direction = directions[0];
- return false;
- } else if (invalidRight()) {
- direction = directions[3];
- return false;
- }
- row--;
- col++;
- direction = directions[2];
- return true;
- }
- private static boolean moveUpLeft() {
- if (invalidUp() && invalidLeft()) {
- Exit();
- } else if (invalidUp()) {
- direction = directions[1];
- return false;
- } else if (invalidLeft()) {
- direction = directions[2];
- return false;
- }
- row--;
- col--;
- direction = directions[3];
- return true;
- }
- private static boolean invalidDown() {
- return row + 1 >= matrix.length;
- }
- private static boolean invalidUp() {
- return row - 1 < 0;
- }
- private static boolean invalidRight() {
- return col + 1 >= matrix[0].length;
- }
- private static boolean invalidLeft() {
- return col - 1 < 0;
- }
- private static void Exit(){
- System.out.println(sum);
- System.exit(0);
- }
- private static void initMatrix() {
- int firstInRow = 1;
- for (int r = 0; r < matrix.length; r++) {
- matrix[r][0] = firstInRow;
- firstInRow *= 2;
- for (int c = 1; c < matrix[r].length; c++) {
- matrix[r][c] = 2 * matrix[r][c - 1];
- }
- }
- }
- private static void printMatrix() {
- for (int i = 0; i < matrix.length; i++) {
- System.out.println(Arrays.toString(matrix[i]));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement