Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _5._Rubiks_Matrix
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- int[,] matrix = new int[input[0], input[1]];
- int number = 1;
- int comandCount = int.Parse(Console.ReadLine());
- bool isbreak = false;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int column = 0; column < matrix.GetLength(1); column++)
- {
- matrix[row, column] = number++;
- }
- }
- for (int i = 0; i < comandCount; i++)
- {
- string[] comand = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- int index = int.Parse(comand[0]);
- string direction = comand[1];
- int times = int.Parse(comand[2]);
- if (direction == "up")
- {
- for (int count = 0; count < times; count++)
- {
- int temp = matrix[0, index];
- for (int insideCount = 0; insideCount < matrix.GetLength(0) - 1; insideCount++)
- {
- matrix[insideCount, index] = matrix[insideCount + 1, index];
- }
- matrix[matrix.GetLength(0) - 1, index] = temp;
- }
- }
- else if (direction == "down")
- {
- for (int count = 0; count < times; count++)
- {
- int temp = matrix[input[0] - 1, index];
- int secondTemp = 0;
- for (int insideCount = 0; insideCount < matrix.GetLength(0) - 1; insideCount++)
- {
- if (insideCount > 0)
- {
- int thirdTemp = secondTemp;
- secondTemp = matrix[insideCount + 1, index];
- matrix[insideCount + 1, index] = thirdTemp;
- }
- else
- {
- secondTemp = matrix[insideCount + 1, index];
- matrix[insideCount + 1, index] = matrix[insideCount, index];
- }
- }
- matrix[0, index] = temp;
- }
- }
- else if (direction == "left")
- {
- for (int count = 0; count < times; count++)
- {
- int temp = matrix[index, 0];
- for (int insideCount = 0; insideCount < matrix.GetLength(1) - 1; insideCount++)
- {
- matrix[index, insideCount] = matrix[index, insideCount + 1];
- }
- matrix[index, matrix.GetLength(1) - 1] = temp;
- }
- }
- else if (direction == "right")
- {
- for (int count = 0; count < times; count++)
- {
- int temp = matrix[index, input[1] - 1];
- int secondTemp = 0;
- for (int insideCount = 0; insideCount < matrix.GetLength(1) - 1; insideCount++)
- {
- if (insideCount > 0)
- {
- int thirdTemp = secondTemp;
- secondTemp = matrix[index, insideCount + 1];
- matrix[index, insideCount + 1] = thirdTemp;
- }
- else
- {
- secondTemp = matrix[index, insideCount + 1];
- matrix[index, insideCount + 1] = matrix[index, insideCount];
- }
- }
- matrix[index, 0] = temp;
- }
- }
- }
- number = 1;
- for (int rows = 0; rows < matrix.GetLength(0); rows++)
- {
- for (int column = 0; column < matrix.GetLength(1); column++)
- {
- isbreak = false;
- if (matrix[rows, column] == number)
- {
- Console.WriteLine("No swap required");
- }
- else
- {
- for (int secondRow = 0; secondRow < matrix.GetLength(0); secondRow++)
- {
- if (isbreak)
- {
- break;
- }
- for (int secondColumn = 0; secondColumn < matrix.GetLength(1); secondColumn++)
- {
- if (matrix[secondRow, secondColumn] == number)
- {
- int temp = matrix[rows, column];
- Console.WriteLine($"Swap ({rows}, {column}) with ({secondRow}, {secondColumn})");
- matrix[rows, column] = matrix[secondRow, secondColumn];
- matrix[secondRow, secondColumn] = temp;
- isbreak = true;
- break;
- }
- }
- }
- }
- number++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment