Guest User

Untitled

a guest
May 26th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _15.RubikMatrix
  4. {
  5.     class RubikMatrix
  6.     {
  7.         private static int[,] matrix;
  8.  
  9.         static void Main()
  10.         {
  11.             string[] dimenctions = Console.ReadLine().Split(' ');
  12.             int rowsCount = int.Parse(dimenctions[0]);
  13.             int columnsCount = int.Parse(dimenctions[1]);
  14.             int iterations = int.Parse(Console.ReadLine());
  15.  
  16.             matrix = new int[rowsCount, columnsCount];
  17.             for (int row = 0; row < rowsCount; row++)
  18.             {
  19.                 for (int col = 0; col < columnsCount; col++)
  20.                 {
  21.                     matrix[row, col] = columnsCount * row + col + 1;
  22.                 }
  23.             }
  24.  
  25.             for (int i = 0; i < iterations; i++)
  26.             {
  27.                 string[] inputArgs = Console.ReadLine().Split(' ');
  28.                 Rotate(inputArgs);
  29.             }
  30.  
  31.             SwapElements();
  32.         }      
  33.  
  34.         private static void Rotate(string[] inputArgs)
  35.         {
  36.             int dimention = int.Parse(inputArgs[0]);
  37.             string direction = inputArgs[1];
  38.             int moves = int.Parse(inputArgs[2]);
  39.  
  40.             if (direction == "up" || direction == "down")
  41.             {
  42.                 var tempMatrix = (int[,])matrix.Clone();
  43.                 for (int row = 0; row < matrix.GetLength(0); row++)
  44.                 {
  45.                     int newRow = direction == "up" ? (row + matrix.GetLength(0) - (moves % matrix.GetLength(0)))
  46.                         : (row + matrix.GetLength(0) + (moves % matrix.GetLength(0)));
  47.                     newRow %= matrix.GetLength(0);
  48.                     matrix[newRow, dimention] = tempMatrix[row, dimention];
  49.                 }
  50.             }
  51.             else if (direction == "left" || direction == "right")
  52.             {
  53.                 var tempMatrix = (int[,])matrix.Clone();
  54.                 for (int col = 0; col < matrix.GetLength(1); col++)
  55.                 {
  56.                     int newCol = direction == "left" ? (col + matrix.GetLength(1) - (moves % matrix.GetLength(1)))
  57.                         : (col + matrix.GetLength(1) + (moves % matrix.GetLength(1)));
  58.                     newCol %= matrix.GetLength(1);
  59.                     matrix[dimention, newCol] = tempMatrix[dimention, col];
  60.                 }
  61.             }          
  62.         }
  63.  
  64.         private static void SwapElements()
  65.         {
  66.             for (int rowOuther = 0; rowOuther < matrix.GetLength(0); rowOuther++)
  67.             {
  68.                 for (int colOuther = 0; colOuther < matrix.GetLength(1); colOuther++)
  69.                 {
  70.                     int element = matrix.GetLength(1) * rowOuther + colOuther + 1;
  71.                     int currentElement = matrix[rowOuther, colOuther];
  72.                     if (matrix[rowOuther, colOuther] != element)
  73.                     {
  74.                         for (int rowInner = 0; rowInner < matrix.GetLength(0); rowInner++)
  75.                         {
  76.                             for (int colInner = 0; colInner < matrix.GetLength(1); colInner++)
  77.                             {
  78.                                 if (matrix[rowInner, colInner] == element)
  79.                                 {
  80.                                     matrix[rowInner, colInner] = currentElement;
  81.                                     matrix[rowOuther, colOuther] = element;
  82.                                     Console.WriteLine($"Swap ({rowOuther}, {colOuther}) with ({rowInner}, {colInner})");
  83.                                 }
  84.                             }
  85.                         }
  86.                     }
  87.                     else
  88.                     {
  89.                         Console.WriteLine("No swap required");
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment