morgan73

RubikMatrix

Jun 4th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05.RubikMatrix
  6. {
  7.     public class RubikMatrix
  8.     {
  9.         private static int[][] matrix = new int[1][];
  10.         private static int rows = 0;
  11.         private static int cols = 0;
  12.  
  13.         public static void Main()
  14.         {
  15.             var matrixDimensions = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  16.  
  17.             rows = matrixDimensions[0];
  18.             cols = matrixDimensions[1];
  19.             InitMatrix(rows, cols);
  20.  
  21.             int n = int.Parse(Console.ReadLine());
  22.  
  23.             long moves = 0;
  24.             string direction;
  25.             int lineNumber;
  26.  
  27.             for (int i = 0; i < n; i++)
  28.             {
  29.                 var command = Console.ReadLine().Split(' ').ToArray();
  30.                 lineNumber = int.Parse(command[0]);
  31.                 direction = command[1];
  32.                 moves = long.Parse(command[2]);
  33.  
  34.                 switch (direction)
  35.                 {
  36.                     case "up":
  37.                     case "down":
  38.                         moveUpOrDown(lineNumber, moves, direction);
  39.                         break;
  40.                     case "left":
  41.                     case "right":
  42.                         moveLeftOrRight(lineNumber, moves, direction);
  43.                         break;
  44.                 }
  45.             }
  46.  
  47.             RearrangeMatrix();
  48.         }
  49.  
  50.         private static void InitMatrix(int rows, int cols)
  51.         {
  52.             matrix = new int[rows][];
  53.  
  54.             for (int i = 0; i < rows; i++)
  55.             {
  56.                 matrix[i] = new int[cols];
  57.  
  58.                 for (int j = 0; j < cols; j++)
  59.                 {
  60.                     matrix[i][j] = i * cols + (j + 1);
  61.                 }
  62.             }
  63.         }
  64.  
  65.         private static void moveUpOrDown(int line, long moves, string direction)
  66.         {
  67.             int m = (int)(moves % cols);
  68.             var colValues = new Queue<int>();
  69.  
  70.             if (direction == "down")
  71.             {
  72.                 for (int i = rows - 1; i >= 0; i--)
  73.                 {
  74.                     colValues.Enqueue(matrix[i][line]);
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 for (int i = 0; i < rows; i++)
  80.                 {
  81.                     colValues.Enqueue(matrix[i][line]);
  82.                 }
  83.             }
  84.  
  85.             for (int i = 0; i < m; i++)
  86.             {
  87.                 int t = colValues.Dequeue();
  88.                 colValues.Enqueue(t);
  89.             }
  90.  
  91.             if (direction == "down")
  92.             {
  93.                 for (int i = rows - 1; i >= 0; i--)
  94.                 {
  95.                     matrix[i][line] = colValues.Dequeue();
  96.                 }
  97.             }
  98.             else
  99.             {
  100.                 for (int i = 0; i < rows; i++)
  101.                 {
  102.                     matrix[i][line] = colValues.Dequeue();
  103.                 }
  104.             }
  105.         }
  106.  
  107.         private static void moveLeftOrRight(int line, long moves, string direction)
  108.         {
  109.             int m = (int)(moves % cols);
  110.             var colValues = new Queue<int>();
  111.  
  112.             if (direction == "right")
  113.             {
  114.                 for (int i = cols - 1; i >= 0; i--)
  115.                 {
  116.                     colValues.Enqueue(matrix[line][i]);
  117.                 }
  118.             }
  119.             else
  120.             {
  121.                 for (int i = 0; i < cols; i++)
  122.                 {
  123.                     colValues.Enqueue(matrix[line][i]);
  124.                 }
  125.             }
  126.  
  127.             for (int i = 0; i < m; i++)
  128.             {
  129.                 int t = colValues.Dequeue();
  130.                 colValues.Enqueue(t);
  131.             }
  132.  
  133.             if (direction == "right")
  134.             {
  135.                 for (int i = cols - 1; i >= 0; i--)
  136.                 {
  137.                     matrix[line][i] = colValues.Dequeue();
  138.                 }
  139.             }
  140.             else
  141.             {
  142.                 for (int i = 0; i < cols; i++)
  143.                 {
  144.                     matrix[line][i] = colValues.Dequeue();
  145.                 }
  146.             }
  147.         }
  148.  
  149.         public static void RearrangeMatrix()
  150.         {
  151.             for (int i = 0; i < rows; i++)
  152.             {
  153.                 for (int j = 0; j < cols; j++)
  154.                 {
  155.                     int initValue = i * cols + (j + 1);
  156.                     int currentValue = matrix[i][j];
  157.  
  158.                     if (initValue != currentValue)
  159.                     {
  160.                         for (int k = 0; k < rows; k++)
  161.                         {
  162.                             var currentRow = matrix[k];
  163.  
  164.                             int index = Array.IndexOf(currentRow, initValue);
  165.  
  166.                             if (index > -1)
  167.                             {
  168.                                 matrix[k][index] = currentValue;
  169.                                 matrix[i][j] = initValue;
  170.                                 Console.WriteLine($"Swap ({i}, {j}) with ({k}, {index})");
  171.                                 break;
  172.                             }
  173.                         }
  174.                     }
  175.                     else
  176.                     {
  177.                         Console.WriteLine("No swap required");
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.  
  183.     }
  184. }
Add Comment
Please, Sign In to add comment