Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _5._Rubiks_Matrix
- {
- public class Program
- {
- public static void Main()
- {
- var sizes = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- var totalRows = sizes[0];
- var totalCols = sizes[1];
- var value = 1;
- var matrix = new int[totalRows, totalCols];
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- matrix[row, col] = value++;
- }
- }
- var countOfCommands = int.Parse(Console.ReadLine());
- for (int i = 0; i < countOfCommands; i++)
- {
- var args = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- var rowCol = int.Parse(args[0]);
- var direction = args[1];
- var moves = int.Parse(args[2]);
- for (int j = 0; j < moves; j++)
- {
- if (direction == "right" || direction == "left")
- {
- MoveLeftOrRight(matrix, direction, moves, rowCol);
- }
- else if (direction == "up" || direction == "down")
- {
- MoveUpOrDown(matrix, direction, moves, rowCol);
- }
- }
- }
- RearengeMatrix(matrix);
- }
- public static void MoveLeftOrRight(int[,] matrix, string direction, int move, int rowCol)
- {
- switch (direction)
- {
- case "right":
- var lastIndex = matrix[rowCol, matrix.GetLength(1) - 1];
- for (int col = matrix.GetLength(1) - 1; col > 0; col--)
- {
- matrix[rowCol, col] = matrix[rowCol, col - 1];
- }
- matrix[rowCol, 0] = lastIndex;
- break;
- case "left":
- var firstIndex = matrix[rowCol, 0];
- for (int i = 0; i < matrix.GetLength(1) - 1; i++)
- {
- matrix[rowCol, i] = matrix[rowCol, i + 1];
- }
- matrix[rowCol, matrix.GetLength(1) - 1] = firstIndex;
- break;
- }
- }
- public static void MoveUpOrDown (int[,] matrix, string direction, int move, int rowCol)
- {
- switch (direction)
- {
- case "down":
- var lastIndex = matrix[matrix.GetLength(0) - 1, rowCol];
- for (int row = matrix.GetLength(0) - 1; row > 0; row--)
- {
- matrix[row, rowCol] = matrix[row - 1, rowCol];
- }
- matrix[0, rowCol] = lastIndex;
- break;
- case "up":
- var firstIndex = matrix[0, rowCol];
- for (int row = 0; row < matrix.GetLength(0) - 1; row++)
- {
- matrix[row, rowCol] = matrix[row + 1, rowCol];
- }
- matrix[matrix.GetLength(0) - 1, rowCol] = firstIndex;
- break;
- }
- }
- public static void RearengeMatrix(int[,] matrix)
- {
- var value = 1;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (matrix[row,col] == value)
- {
- Console.WriteLine("No swap required");
- value++;
- continue;
- }
- for (int r = 0; r < matrix.GetLength(0); r++)
- {
- for (int c = 0; c < matrix.GetLength(1); c++)
- {
- if (matrix[r, c] == value)
- {
- var temp = matrix[row, col];
- matrix[row, col] = matrix[r, c];
- matrix[r, c] = temp;
- Console.WriteLine($"Swap ({row}, {col}) with ({r}, {c})");
- break;
- }
- }
- }
- value++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement