kidroca

Rubix Matrix

Nov 15th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Arrays;
  4. import java.util.LinkedList;
  5. import java.util.Queue;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  * Created by kidroca on 15.11.2015 ?..
  10.  */
  11. public class Problem3 {
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner scan = new Scanner(System.in);
  15.  
  16.         String[] dimensions = scan.nextLine().split("\\s+");
  17.         int rows = Integer.parseInt(dimensions[0]),
  18.                 cols = Integer.parseInt(dimensions[1]);
  19.  
  20.         int[][] matrix = buildMatrix(rows, cols);
  21.  
  22.         int commandsCount = Integer.parseInt(scan.nextLine());
  23.  
  24.         for (int i = 0; i < commandsCount; i++) {
  25.             String[] command = scan.nextLine().split("\\s+");
  26.             executeCommand(command, matrix);
  27.         }
  28.  
  29.        //printMatrix(matrix);
  30.  
  31.         startSwapAction(matrix);
  32.     }
  33.  
  34.     private static void startSwapAction(int[][] matrix) {
  35.         int position = 1;
  36.  
  37.         for (int i = 0; i < matrix.length; i++) {
  38.             for (int j = 0; j < matrix[i].length; j++) {
  39.                 if (matrix[i][j] == position) {
  40.                     System.out.println("No swap required");
  41.                 } else {
  42.                     int[] index = findIndex(matrix, i, position);
  43.                     if (index != null) {
  44.                         swapValues(matrix, i, j, index[0], index[1]);
  45.                         System.out.println();
  46.                     }
  47.                 }
  48.  
  49.                 position++;
  50.  
  51.                 //printMatrix(matrix);
  52.             }
  53.         }
  54.     }
  55.  
  56.     private static void swapValues(int[][] matrix, int i, int j, int i2, int j2) {
  57.         System.out.printf("Swap (%d, %d) with (%d, %d)", i, j, i2, j2);
  58.         int temp = matrix[i][j];
  59.         matrix[i][j] = matrix[i2][j2];
  60.         matrix[i2][j2] = temp;
  61.     }
  62.  
  63.     private static int[] findIndex(int[][] matrix, int i, int position) {
  64.         for (int j = i; j < matrix.length; j++) {
  65.             for (int k = 0; k < matrix[j].length; k++) {
  66.                 if (matrix[j][k] == position) {
  67.                     return new int[] {j, k};
  68.                 }
  69.             }
  70.         }
  71.  
  72.         return null;
  73.     }
  74.  
  75.     private static void printMatrix(int[][] matrix) {
  76.         for (int i = 0; i < matrix.length; i++) {
  77.             for (int j = 0; j < matrix[i].length; j++) {
  78.                 System.out.printf("%d ", matrix[i][j]);
  79.             }
  80.             System.out.println();
  81.         }
  82.     }
  83.  
  84.     private static void executeCommand(String[] command, int[][] matrix) {
  85.         int rowCol = Integer.parseInt(command[0]);
  86.         int turns = Integer.parseInt(command[2]);
  87.         switch (command[1]) {
  88.             case "up":
  89.                 moveRow(matrix, rowCol, turns, -1);
  90.             break;
  91.             case "down":
  92.                 moveRow(matrix, rowCol, turns, +1);
  93.                 break;
  94.             case "left":
  95.                 moveCol(matrix, rowCol, turns, -1);
  96.                 break;
  97.             case "right":
  98.                 moveCol(matrix, rowCol, turns, +1);
  99.                 break;
  100.         }
  101.     }
  102.  
  103.     private static void moveRow(int[][] matrix, int col, int turns, int operation) {
  104.         turns %= matrix.length;
  105.         if (turns == 0) return;
  106.  
  107.         int next;
  108.  
  109.         if (operation > 0) {
  110.             next = turns;
  111.             ascendingRowSwap(matrix, col, next);
  112.         } else {
  113.             next = matrix.length - turns - 1;
  114.             descendingRowSwap(matrix, col, next);
  115.         }
  116.     }
  117.  
  118.     private static void descendingRowSwap(int[][] matrix, int col, int next) {
  119.         Queue<Integer> arangement = new LinkedList<>();
  120.         for (int i = matrix.length - 1; i >= 0; i--) {
  121.             arangement.add(matrix[i][col]);
  122.         }
  123.  
  124.         int count = matrix.length;
  125.  
  126.         while (count > 0) {
  127.             if (!isWithinRange(next, matrix.length)) {
  128.                 next = matrix.length - 1;
  129.             }
  130.  
  131.             matrix[next][col] = arangement.poll();
  132.             count--;
  133.             next -= 1;
  134.         }
  135.     }
  136.  
  137.     private static void ascendingRowSwap(int[][] matrix, int col, int next) {
  138.         Queue<Integer> arangement = new LinkedList<>();
  139.         for (int i = 0; i < matrix.length; i++) {
  140.             arangement.add(matrix[i][col]);
  141.         }
  142.  
  143.         int count = matrix.length;
  144.  
  145.         while (count > 0) {
  146.             if (!isWithinRange(next, matrix.length)) {
  147.                 next = 0;
  148.             }
  149.  
  150.             matrix[next][col] = arangement.poll();
  151.             count--;
  152.             next += 1;
  153.         }
  154.     }
  155.  
  156.     private static boolean isWithinRange(int next, int length) {
  157.         return 0 <= next && next < length;
  158.     }
  159.  
  160.     private static void moveCol(int[][] matrix, int row, int turns, int operation) {
  161.         turns %= matrix[0].length;
  162.  
  163.         int next;
  164.  
  165.         if (operation > 0) {
  166.             next = turns;
  167.             ascendingColSwap(matrix, row, next);
  168.         } else {
  169.             next = matrix[row].length - turns - 1;
  170.             descendingColSwap(matrix, row, next);
  171.         }
  172.     }
  173.  
  174.     private static void descendingColSwap(int[][] matrix, int row, int next) {
  175.         Queue<Integer> arangement = new LinkedList<>();
  176.         for (int i = matrix[row].length - 1; i >= 0; i--) {
  177.             arangement.add(matrix[row][i]);
  178.         }
  179.  
  180.         int count = matrix[row].length;
  181.  
  182.         while (count > 0) {
  183.             if (!isWithinRange(next, matrix[row].length)) {
  184.                 next = matrix[row].length - 1;
  185.             }
  186.  
  187.             matrix[row][next] = arangement.poll();
  188.             count--;
  189.             next -= 1;
  190.         }
  191.     }
  192.  
  193.     private static void ascendingColSwap(int[][] matrix, int row, int next) {
  194.         Queue<Integer> arangement = new LinkedList<>();
  195.         for (int i = 0; i < matrix[row].length; i++) {
  196.             arangement.add(matrix[row][i]);
  197.         }
  198.  
  199.         int count = matrix[row].length;
  200.  
  201.         while (count > 0) {
  202.             if (!isWithinRange(next, matrix[row].length)) {
  203.                 next = 0;
  204.             }
  205.  
  206.             matrix[row][next] = arangement.poll();
  207.             count--;
  208.             next += 1;
  209.         }
  210.     }
  211.  
  212.     private static int[][] buildMatrix(int rows, int cols) {
  213.         int[][] matrix = new int[rows][];
  214.  
  215.         int counter = 1;
  216.         for (int i = 0; i < rows; i++) {
  217.             matrix[i] = new int[cols];
  218.             for (int j = 0; j < cols; j++) {
  219.                 matrix[i][j] = counter++;
  220.             }
  221.         }
  222.  
  223.         return matrix;
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment