daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Rubiks_Matrix
  6. {
  7.     class Program
  8.     {
  9.  
  10.         static void Main(string[] args)
  11.         {
  12.             var size = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  13.  
  14.             var commandsCount = int.Parse(Console.ReadLine());
  15.  
  16.             var matrix = new int[size[0], size[1]];
  17.  
  18.  
  19.             var counter = 1;
  20.             for (int row = 0; row < matrix.GetLength(0); row++)
  21.             {
  22.  
  23.                 for (int col = 0; col < matrix.GetLength(1); col++)
  24.                 {
  25.                     matrix[row, col] = counter;
  26.                     counter++;
  27.                 }
  28.             }
  29.  
  30.             for (int i = 0; i < commandsCount; i++)
  31.             {
  32.                 var command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  33.  
  34.                 var rowOrCol = int.Parse(command[0]);
  35.                 var direction = command[1];
  36.                 var moves = int.Parse(command[2]);
  37.  
  38.                 switch (command[1])
  39.                 {
  40.                     case "left":
  41.                         var colsLeft = matrix.GetLength(1);
  42.                         var restLeft = moves % colsLeft;
  43.                         var rowLeft = new Queue<int>();
  44.                         for (int h = 0; h < colsLeft; h++)
  45.                         {
  46.                             rowLeft.Enqueue(matrix[rowOrCol, h]);
  47.                         }
  48.                         for (int c = 0; c < restLeft; c++)
  49.                         {
  50.                             rowLeft.Enqueue(rowLeft.Dequeue());
  51.                         }
  52.                         for (int d = 0; d < colsLeft; d++)
  53.                         {
  54.                             matrix[rowOrCol, d] = rowLeft.Dequeue();
  55.                         }
  56.                         break;
  57.                     case "right":
  58.                         var colsRight = matrix.GetLength(1);
  59.                         var restRight = moves % colsRight;
  60.                         var rowRight = new Queue<int>();
  61.                         for (int f = colsRight - 1; f >= 0; f--)
  62.                         {
  63.                             rowRight.Enqueue(matrix[rowOrCol, f]);
  64.                         }
  65.                         for (int g = 0; g < restRight; g++)
  66.                         {
  67.                             rowRight.Enqueue(rowRight.Dequeue());
  68.                         }
  69.                         for (int e = colsRight - 1; e >= 0; e--)
  70.                         {
  71.                             matrix[rowOrCol, e] = rowRight.Dequeue();
  72.                         }
  73.                         break;
  74.                     case "down":
  75.                         int rowsDown = matrix.GetLength(0);
  76.                         int restDown = moves % rowsDown;
  77.                         Queue<int> columnDown = new Queue<int>();
  78.                         for (int row = (rowsDown - 1); row >= 0; row--)
  79.                         {
  80.                                 columnDown.Enqueue(matrix[row, rowOrCol]);
  81.                         }
  82.                         for (int j = 0; j < restDown; j++)
  83.                         {
  84.                             columnDown.Enqueue(columnDown.Dequeue());
  85.                         }
  86.                         for (int k = (rowsDown - 1); k >= 0; k--)
  87.                         {
  88.                             matrix[k, rowOrCol] = columnDown.Dequeue();
  89.                         }
  90.                         break;
  91.                     case "up":
  92.                         int rowsUp = matrix.GetLength(0);
  93.                         int restUp = moves % rowsUp;
  94.                         Queue<int> columnUp = new Queue<int>();
  95.                         for (int row = 0; row < rowsUp; row++)
  96.                         {
  97.                                 columnUp.Enqueue(matrix[row, rowOrCol]);                          
  98.                         }
  99.                         for (int j = 0; j < restUp; j++)
  100.                         {
  101.                             columnUp.Enqueue(columnUp.Dequeue());
  102.                         }
  103.                         for (int k = 0; k < matrix.GetLength(0); k++)
  104.                         {
  105.                             matrix[k, rowOrCol] = columnUp.Dequeue();
  106.                         }
  107.                         break;
  108.                 }
  109.             }
  110.             var expected = 1;
  111.             for (int row = 0; row < matrix.GetLength(0); row++)
  112.             {
  113.                 for (int col = 0; col < matrix.GetLength(1); col++)
  114.                 {
  115.                     if (matrix[row,col]==expected)
  116.                     {
  117.                         Console.WriteLine("No swap required");
  118.                         expected++;
  119.                         continue;
  120.                     }
  121.                     for (int r = 0; r < matrix.GetLength(0); r++)
  122.                     {
  123.                         for (int c = 0; c < matrix.GetLength(1); c++)
  124.                         {  
  125.                             if (matrix[r, c] == expected)
  126.                             {
  127.                                 var temp = matrix[row, col];
  128.                                 matrix[row,col]=matrix[r,c];
  129.                                 matrix[r, c] = temp;
  130.                                 Console.WriteLine($"Swap ({row}, {col}) with ({r}, {c})");
  131.                                
  132.                                 break;
  133.                             }
  134.                         }
  135.                     }
  136.                     expected++;
  137.                 }
  138.             }
  139.         }
  140.     }
  141. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top