Advertisement
simonradev

Rubik'sMatrix

Mar 14th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.29 KB | None | 0 0
  1. namespace RubiksCube
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class RubiksCube
  8.     {
  9.         public static int[,] rubicsCube;
  10.  
  11.         public static void Main()
  12.         {
  13.             int[] dimansions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.  
  15.             int rows = dimansions[0];
  16.             int cols = dimansions[1];
  17.  
  18.             rubicsCube = new int[rows, cols];
  19.  
  20.             int numberFiller = 0;
  21.             for (int currRow = 0; currRow < rubicsCube.GetLength(0); currRow++)
  22.             {
  23.                 for (int currCol = 0; currCol < rubicsCube.GetLength(1); currCol++)
  24.                 {
  25.                     rubicsCube[currRow, currCol] = ++numberFiller;
  26.                 }
  27.             }
  28.  
  29.             int numberOfInputs = int.Parse(Console.ReadLine());
  30.             for (int currInput = 0; currInput < numberOfInputs; currInput++)
  31.             {
  32.                 string[] commandInfo = Console.ReadLine().Split().ToArray();
  33.  
  34.                 int rowOrColumn = int.Parse(commandInfo[0]);
  35.                 string direction = commandInfo[1];
  36.                 int count = int.Parse(commandInfo[2]);
  37.  
  38.                 switch (direction)
  39.                 {
  40.                     case "up":
  41.                         MoveColUp(rowOrColumn, count);
  42.                         break;
  43.                     case "down":
  44.                         MoveColDown(rowOrColumn, count);
  45.                         break;
  46.                     case "left":
  47.                         MoveRowLeft(rowOrColumn, count);
  48.                         break;
  49.                     case "right":
  50.                         MoveRowRight(rowOrColumn, count);
  51.                         break;
  52.  
  53.                     default:
  54.                         break;
  55.                 }
  56.                
  57.             }
  58.  
  59.             StringBuilder result = new StringBuilder();
  60.             for (int currRow = 0; currRow < rubicsCube.GetLength(0); currRow++)
  61.             {
  62.                 for (int currCol = 0; currCol < rubicsCube.GetLength(1); currCol++)
  63.                 {
  64.                     int number = rubicsCube[currRow, currCol];
  65.  
  66.                     int minNumber = number;
  67.                     int rowToSwitchWith = int.MaxValue;
  68.                     int colToSwitchWith = int.MaxValue;
  69.                     for (int nextRow = currRow; nextRow < rubicsCube.GetLength(0); nextRow++)
  70.                     {
  71.                         int startCol = nextRow == currRow ? currCol : 0 ;
  72.  
  73.                         for (int nextCol = startCol; nextCol < rubicsCube.GetLength(1); nextCol++)
  74.                         {
  75.                             int nextNumber = rubicsCube[nextRow, nextCol];
  76.  
  77.                             if (nextNumber < minNumber)
  78.                             {
  79.                                 minNumber = nextNumber;
  80.  
  81.                                 rowToSwitchWith = nextRow;
  82.                                 colToSwitchWith = nextCol;
  83.                             }
  84.                         }
  85.                     }
  86.  
  87.                     if (minNumber != number)
  88.                     {
  89.                         int elementHolder = rubicsCube[currRow, currCol];
  90.                         rubicsCube[currRow, currCol] = rubicsCube[rowToSwitchWith, colToSwitchWith];
  91.                         rubicsCube[rowToSwitchWith, colToSwitchWith] = elementHolder;
  92.  
  93.                         result.AppendLine($"Swap ({currRow}, {currCol}) with ({rowToSwitchWith}, {colToSwitchWith})");
  94.                     }
  95.                     else
  96.                     {
  97.                         result.AppendLine($"No swap required");
  98.                     }
  99.                 }
  100.             }
  101.  
  102.             Console.WriteLine(result.ToString().TrimEnd('\r', '\n'));
  103.         }
  104.  
  105.         private static void MoveRowRight(int rowOrColumn, int count)
  106.         {
  107.             int row = rowOrColumn;
  108.             count = count % rubicsCube.GetLength(1);
  109.  
  110.             for (int currRotation = 0; currRotation < count; currRotation++)
  111.             {
  112.                 int elementHolder = rubicsCube[row, rubicsCube.GetLength(1) - 1];
  113.  
  114.                 for (int currCol = rubicsCube.GetLength(1) - 1; currCol >= 1; currCol--)
  115.                 {
  116.                     rubicsCube[row, currCol] = rubicsCube[row, currCol - 1];
  117.                 }
  118.  
  119.                 rubicsCube[row, 0] = elementHolder;
  120.             }
  121.         }
  122.  
  123.         private static void MoveRowLeft(int rowOrColumn, int count)
  124.         {
  125.             int row = rowOrColumn;
  126.             count = count % rubicsCube.GetLength(1);
  127.  
  128.             for (int currRotation = 0; currRotation < count; currRotation++)
  129.             {
  130.                 int elementHolder = rubicsCube[row, 0];
  131.  
  132.                 for (int currCol = 0; currCol < rubicsCube.GetLength(1) - 1; currCol++)
  133.                 {
  134.                     rubicsCube[row, currCol] = rubicsCube[row, currCol + 1];
  135.                 }
  136.  
  137.                 rubicsCube[row, rubicsCube.GetLength(1) - 1] = elementHolder;
  138.             }
  139.         }
  140.  
  141.         private static void MoveColDown(int rowOrColumn, int count)
  142.         {
  143.             int colToMove = rowOrColumn;
  144.             count = count % rubicsCube.GetLength(0);
  145.  
  146.             for (int currRotation = 0; currRotation < count; currRotation++)
  147.             {
  148.                 int elementHolder = rubicsCube[rubicsCube.GetLength(0) - 1, colToMove];
  149.  
  150.                 for (int currRow = rubicsCube.GetLength(0) - 1; currRow >= 1; currRow--)
  151.                 {
  152.                     rubicsCube[currRow, colToMove] = rubicsCube[currRow - 1, colToMove];
  153.                 }
  154.  
  155.                 rubicsCube[0, colToMove] = elementHolder;
  156.             }
  157.         }
  158.  
  159.         private static void MoveColUp(int rowOrColumn, int count)
  160.         {
  161.             int colToMove = rowOrColumn;
  162.             count = count % rubicsCube.GetLength(0);
  163.  
  164.             for (int currRotation = 0; currRotation < count; currRotation++)
  165.             {
  166.                 int elementHolder = rubicsCube[0, colToMove];
  167.  
  168.                 for (int currRow = 0; currRow < rubicsCube.GetLength(0) - 1; currRow++)
  169.                 {
  170.                     rubicsCube[currRow, colToMove] = rubicsCube[currRow + 1, colToMove];
  171.                 }
  172.  
  173.                 rubicsCube[rubicsCube.GetLength(0) - 1, colToMove] = elementHolder;
  174.             }
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement