Advertisement
Guest User

Rubik's matrix

a guest
Nov 15th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class RubikMatrix {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         String[] input = sc.nextLine().split("\\s+");
  7.         int rows = Integer.parseInt(input[0]);
  8.         int cols = Integer.parseInt(input[1]);
  9.         int moves = 0;
  10.         int[][] matrix = new int[rows][cols];
  11.         for (int i = 0; i < rows; i++) {
  12.             for (int j = 0; j < cols; j++) {
  13.                 matrix[i][j] = i*cols + j+1;
  14.             }
  15.         }
  16.         int N = Integer.parseInt(sc.nextLine());
  17.         int rounds;
  18.         for (int i = 0; i < N; i++) {
  19.             input = sc.nextLine().split("\\s+");
  20.             rounds = Integer.parseInt(input[2]);
  21.             if (input[1].equals("up") || input[1].equals("down")) {
  22.                 int col = Integer.parseInt(input[0]);
  23.                 if (input[1].equals("up")) {
  24.                     swapCol(matrix, col, -rounds);
  25.                 } else {
  26.                     swapCol(matrix, col, rounds);
  27.                 }
  28.  
  29.             } else {
  30.                 int row = Integer.parseInt(input[0]);
  31.                 if (input[1].equals("left")) {
  32.                     swapRow(matrix, row, -rounds);
  33.                 } else {
  34.                     swapRow(matrix, row, rounds);
  35.                 }
  36.             }
  37.         }
  38.         check(matrix);
  39.     }
  40.     private static void swapRow (int[][] matrix, int row, int turns ) {
  41.         List<Integer> lineRow = new ArrayList<>();
  42.         for (int i = 0; i < matrix[row].length; i++) {
  43.             lineRow.add(matrix[row][i]);
  44.         }
  45.         turns %= matrix[0].length;
  46.         if (turns < 0) {
  47.             turns += matrix[0].length;
  48.         }
  49.         List<Integer> firstHalf = lineRow.subList(0,lineRow.size()- turns);
  50.         List<Integer> secondHalf = lineRow.subList(lineRow.size() - turns, lineRow.size());
  51.         secondHalf.addAll(firstHalf);
  52.         for (int i = 0; i < matrix[row].length; i++) {
  53.             matrix[row][i] = secondHalf.get(i);
  54.         }
  55.     }
  56.     private static void swapCol (int[][] matrix, int col, int turns ) {
  57.         List<Integer> lineRow = new ArrayList<>();
  58.         for (int i = 0; i < matrix.length; i++) {
  59.             lineRow.add(matrix[i][col]);
  60.         }
  61.         turns %= matrix.length;
  62.         if (turns < 0) {
  63.             turns += matrix.length;
  64.         }
  65.         List<Integer> firstHalf = lineRow.subList(0,lineRow.size()-turns);
  66.         List<Integer> secondHalf = lineRow.subList(lineRow.size() -turns, lineRow.size());
  67.         secondHalf.addAll(firstHalf);
  68.         for (int i = 0; i < matrix.length; i++) {
  69.             matrix[i][col] = secondHalf.get(i);
  70.         }
  71.     }
  72.     private static void check(int[][] matrix) {
  73.  
  74.         for (int i = 0; i < matrix.length; i++) {
  75.             for (int j = 0; j < matrix[0].length; j++) {
  76.                 int value = i*matrix[0].length + j+1;
  77.                 if (matrix[i][j] == value) {
  78.                     System.out.println("No swap required");
  79.                 } else {
  80.                     int[] coordinates = searchElement(matrix, value);
  81.                     System.out.printf("Swap (%d %d) with (%d %d)\n", i, j, coordinates[0], coordinates[1]);
  82.                     int temp = matrix[i][j];
  83.                     matrix[i][j] = matrix[coordinates[0]][coordinates[1]];
  84.                     matrix[coordinates[0]][coordinates[1]] = temp;
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     private static int[] searchElement(int[][] matrix, int value) {
  91.         int[] coordinates = new int[2];
  92.         for (int i = 0; i < matrix.length; i++) {
  93.             for (int j = 0; j < matrix[0].length; j++) {
  94.                 if(matrix[i][j] == value) {
  95.                     coordinates[0] = i;
  96.                     coordinates[1] = j;
  97.                     return coordinates;
  98.                 }
  99.             }
  100.         }
  101.         return new int[]{-1, -1};
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement