Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace RubiksCube
- {
- using System;
- using System.Linq;
- using System.Text;
- public class RubiksCube
- {
- public static int[,] rubicsCube;
- public static void Main()
- {
- int[] dimansions = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int rows = dimansions[0];
- int cols = dimansions[1];
- rubicsCube = new int[rows, cols];
- int numberFiller = 0;
- for (int currRow = 0; currRow < rubicsCube.GetLength(0); currRow++)
- {
- for (int currCol = 0; currCol < rubicsCube.GetLength(1); currCol++)
- {
- rubicsCube[currRow, currCol] = ++numberFiller;
- }
- }
- int numberOfInputs = int.Parse(Console.ReadLine());
- for (int currInput = 0; currInput < numberOfInputs; currInput++)
- {
- string[] commandInfo = Console.ReadLine().Split().ToArray();
- int rowOrColumn = int.Parse(commandInfo[0]);
- string direction = commandInfo[1];
- int count = int.Parse(commandInfo[2]);
- switch (direction)
- {
- case "up":
- MoveColUp(rowOrColumn, count);
- break;
- case "down":
- MoveColDown(rowOrColumn, count);
- break;
- case "left":
- MoveRowLeft(rowOrColumn, count);
- break;
- case "right":
- MoveRowRight(rowOrColumn, count);
- break;
- default:
- break;
- }
- }
- StringBuilder result = new StringBuilder();
- for (int currRow = 0; currRow < rubicsCube.GetLength(0); currRow++)
- {
- for (int currCol = 0; currCol < rubicsCube.GetLength(1); currCol++)
- {
- int number = rubicsCube[currRow, currCol];
- int minNumber = number;
- int rowToSwitchWith = int.MaxValue;
- int colToSwitchWith = int.MaxValue;
- for (int nextRow = currRow; nextRow < rubicsCube.GetLength(0); nextRow++)
- {
- int startCol = nextRow == currRow ? currCol : 0 ;
- for (int nextCol = startCol; nextCol < rubicsCube.GetLength(1); nextCol++)
- {
- int nextNumber = rubicsCube[nextRow, nextCol];
- if (nextNumber < minNumber)
- {
- minNumber = nextNumber;
- rowToSwitchWith = nextRow;
- colToSwitchWith = nextCol;
- }
- }
- }
- if (minNumber != number)
- {
- int elementHolder = rubicsCube[currRow, currCol];
- rubicsCube[currRow, currCol] = rubicsCube[rowToSwitchWith, colToSwitchWith];
- rubicsCube[rowToSwitchWith, colToSwitchWith] = elementHolder;
- result.AppendLine($"Swap ({currRow}, {currCol}) with ({rowToSwitchWith}, {colToSwitchWith})");
- }
- else
- {
- result.AppendLine($"No swap required");
- }
- }
- }
- Console.WriteLine(result.ToString().TrimEnd('\r', '\n'));
- }
- private static void MoveRowRight(int rowOrColumn, int count)
- {
- int row = rowOrColumn;
- count = count % rubicsCube.GetLength(1);
- for (int currRotation = 0; currRotation < count; currRotation++)
- {
- int elementHolder = rubicsCube[row, rubicsCube.GetLength(1) - 1];
- for (int currCol = rubicsCube.GetLength(1) - 1; currCol >= 1; currCol--)
- {
- rubicsCube[row, currCol] = rubicsCube[row, currCol - 1];
- }
- rubicsCube[row, 0] = elementHolder;
- }
- }
- private static void MoveRowLeft(int rowOrColumn, int count)
- {
- int row = rowOrColumn;
- count = count % rubicsCube.GetLength(1);
- for (int currRotation = 0; currRotation < count; currRotation++)
- {
- int elementHolder = rubicsCube[row, 0];
- for (int currCol = 0; currCol < rubicsCube.GetLength(1) - 1; currCol++)
- {
- rubicsCube[row, currCol] = rubicsCube[row, currCol + 1];
- }
- rubicsCube[row, rubicsCube.GetLength(1) - 1] = elementHolder;
- }
- }
- private static void MoveColDown(int rowOrColumn, int count)
- {
- int colToMove = rowOrColumn;
- count = count % rubicsCube.GetLength(0);
- for (int currRotation = 0; currRotation < count; currRotation++)
- {
- int elementHolder = rubicsCube[rubicsCube.GetLength(0) - 1, colToMove];
- for (int currRow = rubicsCube.GetLength(0) - 1; currRow >= 1; currRow--)
- {
- rubicsCube[currRow, colToMove] = rubicsCube[currRow - 1, colToMove];
- }
- rubicsCube[0, colToMove] = elementHolder;
- }
- }
- private static void MoveColUp(int rowOrColumn, int count)
- {
- int colToMove = rowOrColumn;
- count = count % rubicsCube.GetLength(0);
- for (int currRotation = 0; currRotation < count; currRotation++)
- {
- int elementHolder = rubicsCube[0, colToMove];
- for (int currRow = 0; currRow < rubicsCube.GetLength(0) - 1; currRow++)
- {
- rubicsCube[currRow, colToMove] = rubicsCube[currRow + 1, colToMove];
- }
- rubicsCube[rubicsCube.GetLength(0) - 1, colToMove] = elementHolder;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement