Mishakis

Bounce

Nov 24th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.         int n = in.nextInt();
  8.         int m = in.nextInt();
  9.  
  10.  
  11.         long[][] matrix = new long[n][m];
  12.  
  13.         long startValue = 1;
  14.         for (int row = 0; row < n; row++) {
  15.             for (int col = 0; col < m; col++) {
  16.                 matrix[row][col] = startValue;
  17.                 if (col < m - 1) {
  18.                     startValue *= 2;
  19.                 }
  20.             }
  21.             startValue = matrix[row][1];
  22.         }
  23.  
  24.         int dirRow = 1;
  25.         int dirCol = 1;
  26.         int currentRow = 0;
  27.         int currentCol = 0;
  28.         long sum = matrix[currentRow][currentCol];
  29.         int nextRow = 0;
  30.         int nextCol = 0;
  31.  
  32.         if (n == 1 || m == 1) {
  33.             System.out.println(matrix[0][0]);
  34.         } else {
  35.             while (true) {
  36.                 nextRow = currentRow + dirRow;
  37.                 nextCol = currentCol + dirCol;
  38.                 if ((nextRow == 0 || nextRow == n - 1) && (nextCol == 0 || nextCol == m - 1)) {
  39.                     sum += matrix[nextRow][nextCol];
  40.                     System.out.println(sum);
  41.                     break;
  42.                 }
  43.  
  44.                 if (nextRow == 0 || nextRow == n - 1) {
  45.                     dirRow *= -1;
  46.                 }
  47.                 if (nextCol == 0 || nextCol == m - 1) {
  48.                     dirCol *= -1;
  49.                 }
  50.  
  51.                 currentRow = nextRow;
  52.                 currentCol = nextCol;
  53.                 sum += matrix[currentRow][currentCol];
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment