Advertisement
social1986

Untitled

Jan 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _5._Rubiks_Matrix
  5. {
  6.     public class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             var sizes = Console.ReadLine()
  11.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.  
  15.             var totalRows = sizes[0];
  16.             var totalCols = sizes[1];
  17.             var value = 1;
  18.             var matrix = new int[totalRows, totalCols];
  19.  
  20.             for (int row = 0; row < matrix.GetLength(0); row++)
  21.             {
  22.                 for (int col = 0; col < matrix.GetLength(1); col++)
  23.                 {
  24.                     matrix[row, col] = value++;
  25.                 }
  26.             }
  27.  
  28.             var countOfCommands = int.Parse(Console.ReadLine());
  29.  
  30.             for (int i = 0; i < countOfCommands; i++)
  31.             {
  32.                 var args = Console.ReadLine()
  33.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  34.                 var rowCol = int.Parse(args[0]);
  35.                 var direction = args[1];
  36.                 var moves = int.Parse(args[2]);
  37.  
  38.                 for (int j = 0; j < moves; j++)
  39.                 {
  40.                     if (direction == "right" || direction == "left")
  41.                     {
  42.                         MoveLeftOrRight(matrix, direction, moves, rowCol);
  43.                     }
  44.                     else if (direction == "up" || direction == "down")
  45.                     {
  46.                         MoveUpOrDown(matrix, direction, moves, rowCol);
  47.                     }
  48.                 }
  49.             }
  50.             RearengeMatrix(matrix);
  51.  
  52.         }
  53.  
  54.         public static void MoveLeftOrRight(int[,] matrix, string direction, int move, int rowCol)
  55.         {
  56.             switch (direction)
  57.             {
  58.                 case "right":
  59.                     var lastIndex = matrix[rowCol, matrix.GetLength(1) - 1];
  60.                     for (int col = matrix.GetLength(1) - 1; col > 0; col--)
  61.                     {
  62.                         matrix[rowCol, col] = matrix[rowCol, col - 1];
  63.                     }
  64.                     matrix[rowCol, 0] = lastIndex;
  65.                     break;
  66.                 case "left":
  67.                     var firstIndex = matrix[rowCol, 0];
  68.                     for (int i = 0; i < matrix.GetLength(1) - 1; i++)
  69.                     {
  70.                         matrix[rowCol, i] = matrix[rowCol, i + 1];
  71.                     }
  72.                     matrix[rowCol, matrix.GetLength(1) - 1] = firstIndex;
  73.                     break;
  74.             }
  75.         }
  76.  
  77.         public static void MoveUpOrDown (int[,] matrix, string direction, int move, int rowCol)
  78.         {
  79.             switch (direction)
  80.             {
  81.                 case "down":
  82.                     var lastIndex = matrix[matrix.GetLength(0) - 1, rowCol];
  83.  
  84.                     for (int row = matrix.GetLength(0) - 1; row > 0; row--)
  85.                     {
  86.                         matrix[row, rowCol] = matrix[row - 1, rowCol];
  87.                     }
  88.  
  89.                     matrix[0, rowCol] = lastIndex;
  90.                     break;
  91.  
  92.                 case "up":
  93.                     var firstIndex = matrix[0, rowCol];
  94.  
  95.                     for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  96.                     {
  97.                         matrix[row, rowCol] = matrix[row + 1, rowCol];
  98.                     }
  99.  
  100.                     matrix[matrix.GetLength(0) - 1, rowCol] = firstIndex;
  101.                     break;
  102.             }
  103.         }
  104.  
  105.         public static void RearengeMatrix(int[,] matrix)
  106.         {
  107.             var value = 1;
  108.             for (int row = 0; row < matrix.GetLength(0); row++)
  109.             {
  110.                 for (int col = 0; col < matrix.GetLength(1); col++)
  111.                 {
  112.                     if (matrix[row,col] == value)
  113.                     {
  114.                         Console.WriteLine("No swap required");
  115.                         value++;
  116.                         continue;
  117.                     }
  118.  
  119.                     for (int r = 0; r < matrix.GetLength(0); r++)
  120.                     {
  121.                         for (int c = 0; c < matrix.GetLength(1); c++)
  122.                         {
  123.                             if (matrix[r, c] == value)
  124.                             {
  125.                                 var temp = matrix[row, col];
  126.                                 matrix[row, col] = matrix[r, c];
  127.                                 matrix[r, c] = temp;
  128.                                 Console.WriteLine($"Swap ({row}, {col}) with ({r}, {c})");
  129.                                 break;
  130.                             }
  131.                         }
  132.                     }
  133.                     value++;
  134.                 }
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement