vstoyanov

rubix matrix

Feb 2nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _5._Rubiks_Matrix
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  11.  
  12.             int[,] matrix = new int[input[0], input[1]];
  13.  
  14.             int number = 1;
  15.             int comandCount = int.Parse(Console.ReadLine());
  16.  
  17.  
  18.             bool isbreak = false;
  19.  
  20.  
  21.             for (int row = 0; row < matrix.GetLength(0); row++)
  22.             {
  23.                 for (int column = 0; column < matrix.GetLength(1); column++)
  24.                 {
  25.                     matrix[row, column] = number++;
  26.                 }
  27.             }
  28.  
  29.             for (int i = 0; i < comandCount; i++)
  30.             {
  31.                 string[] comand = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  32.  
  33.                 int index = int.Parse(comand[0]);
  34.  
  35.                 string direction = comand[1];
  36.  
  37.                 int times = int.Parse(comand[2]);
  38.  
  39.  
  40.                 if (direction == "up")
  41.                 {
  42.                     for (int count = 0; count < times; count++)
  43.                     {
  44.  
  45.                         int temp = matrix[0, index];
  46.                         for (int insideCount = 0; insideCount < matrix.GetLength(0) - 1; insideCount++)
  47.                         {
  48.                             matrix[insideCount, index] = matrix[insideCount + 1, index];
  49.  
  50.                         }
  51.  
  52.                         matrix[matrix.GetLength(0) - 1, index] = temp;
  53.  
  54.                     }
  55.                 }
  56.                 else if (direction == "down")
  57.                 {
  58.                     for (int count = 0; count < times; count++)
  59.                     {
  60.  
  61.                         int temp = matrix[input[0] - 1, index];
  62.                         int secondTemp = 0;
  63.                         for (int insideCount = 0; insideCount < matrix.GetLength(0) - 1; insideCount++)
  64.                         {
  65.  
  66.  
  67.  
  68.                             if (insideCount > 0)
  69.                             {
  70.                                 int thirdTemp = secondTemp;
  71.                                 secondTemp = matrix[insideCount + 1, index];
  72.                                 matrix[insideCount + 1, index] = thirdTemp;
  73.  
  74.                             }
  75.                             else
  76.                             {
  77.                                 secondTemp = matrix[insideCount + 1, index];
  78.  
  79.                                 matrix[insideCount + 1, index] = matrix[insideCount, index];
  80.  
  81.                             }
  82.                         }
  83.  
  84.                         matrix[0, index] = temp;
  85.  
  86.                     }
  87.                 }
  88.                 else if (direction == "left")
  89.                 {
  90.                     for (int count = 0; count < times; count++)
  91.                     {
  92.                         int temp = matrix[index, 0];
  93.  
  94.                         for (int insideCount = 0; insideCount < matrix.GetLength(1) - 1; insideCount++)
  95.                         {
  96.                             matrix[index, insideCount] = matrix[index, insideCount + 1];
  97.                         }
  98.                         matrix[index, matrix.GetLength(1) - 1] = temp;
  99.  
  100.                     }
  101.                 }
  102.                 else if (direction == "right")
  103.                 {
  104.                     for (int count = 0; count < times; count++)
  105.                     {
  106.  
  107.                         int temp = matrix[index, input[1] - 1];
  108.                         int secondTemp = 0;
  109.                         for (int insideCount = 0; insideCount < matrix.GetLength(1) - 1; insideCount++)
  110.                         {
  111.                             if (insideCount > 0)
  112.                             {
  113.                                 int thirdTemp = secondTemp;
  114.                                 secondTemp = matrix[index, insideCount + 1];
  115.                                 matrix[index, insideCount + 1] = thirdTemp;
  116.  
  117.  
  118.  
  119.                             }
  120.                             else
  121.                             {
  122.                                 secondTemp = matrix[index, insideCount + 1];
  123.                                 matrix[index, insideCount + 1] = matrix[index, insideCount];
  124.  
  125.                             }
  126.  
  127.  
  128.  
  129.  
  130.                         }
  131.                         matrix[index, 0] = temp;
  132.  
  133.                     }
  134.                 }
  135.  
  136.  
  137.             }
  138.             number = 1;
  139.             for (int rows = 0; rows < matrix.GetLength(0); rows++)
  140.             {
  141.                 for (int column = 0; column < matrix.GetLength(1); column++)
  142.                 {
  143.                     isbreak = false;
  144.                     if (matrix[rows, column] == number)
  145.                     {
  146.                         Console.WriteLine("No swap required");
  147.                     }
  148.                     else
  149.                     {
  150.                         for (int secondRow = 0; secondRow < matrix.GetLength(0); secondRow++)
  151.                         {
  152.                             if (isbreak)
  153.                             {
  154.                                 break;
  155.                             }
  156.  
  157.                             for (int secondColumn = 0; secondColumn < matrix.GetLength(1); secondColumn++)
  158.                             {
  159.                                 if (matrix[secondRow, secondColumn] == number)
  160.                                 {
  161.                                     int temp = matrix[rows, column];
  162.  
  163.                                     Console.WriteLine($"Swap ({rows}, {column}) with ({secondRow}, {secondColumn})");
  164.                                     matrix[rows, column] = matrix[secondRow, secondColumn];
  165.                                     matrix[secondRow, secondColumn] = temp;
  166.  
  167.                                     isbreak = true;
  168.                                     break;
  169.  
  170.                                 }
  171.                             }
  172.  
  173.  
  174.  
  175.                         }
  176.                     }
  177.                     number++;
  178.                 }
  179.             }
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment