Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- public class RubiksMatrix
- {
- public static void Main()
- {
- int[] dimensions = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- int n = int.Parse(Console.ReadLine());
- int rows = dimensions[0];
- int cols = dimensions[1];
- int[][] matrix = new int[rows][];
- FillMatrix(matrix, rows, cols);
- for (int i = 0; i < n; i++)
- {
- string[] commandAgs = Console.ReadLine().Split(' ');
- int rowOrCol = int.Parse(commandAgs[0]);
- string direction = commandAgs[1];
- int moves = int.Parse(commandAgs[2]);
- if (direction == "left")
- {
- SwapToLeft(cols, matrix, rowOrCol, moves, rows);
- }
- else if (direction == "right")
- {
- SwapToRight(cols, matrix, rowOrCol, moves, rows);
- }
- else if (direction == "up")
- {
- SwapedUp(rows, matrix, rowOrCol, moves, cols);
- }
- else if (direction == "down")
- {
- SwapDown(rows, matrix, rowOrCol, moves, cols);
- }
- }
- int m = 1;
- for (int currRow = 0; currRow < rows; currRow++)
- {
- for (int currCol = 0; currCol < cols; currCol++)
- {
- if (matrix[currRow][currCol] == currRow + currCol + m)
- {
- Console.WriteLine("No swap required");
- }
- else
- {
- int currPosValue = matrix[currRow][currCol];
- int currPosRow = currRow;
- int currPosCol = currCol;
- int nextPosRow = 0;
- int nextPosCol = 0;
- int nextPosValue = 0;
- for (int r = 0; r < rows; r++)
- {
- for (int c = 0; c < cols; c++)
- {
- if ((matrix[r][c] == currRow + currCol + m) && (matrix[currRow][currCol] != currRow + currCol + m))
- {
- nextPosRow = r;
- nextPosCol = c;
- nextPosValue = matrix[r][c];
- matrix[currPosRow][currPosCol] = nextPosValue;
- matrix[nextPosRow][nextPosCol] = currPosValue;
- break;
- }
- }
- }
- Console.WriteLine($"Swap ({currPosRow}, {currPosCol}) with ({nextPosRow}, {nextPosCol})");
- }
- }
- m += 2;
- }
- }
- public static void FillMatrix(int[][] matrix, int rows, int cols)
- {
- int count = 1;
- for (int row = 0; row < rows; row++)
- {
- matrix[row] = new int[cols];
- for (int col = 0; col < cols; col++)
- {
- matrix[row][col] = count;
- count++;
- }
- }
- }
- public static void SwapDown(int rows, int[][] matrix, int rowOrCol, int moves, int cols)
- {
- int[] swapedCol = new int[rows];
- for (int currSwap = 0; currSwap < moves % cols; currSwap++)
- {
- int bottomElement = matrix[rows - 1][rowOrCol];
- for (int j = 0; j < swapedCol.Length - 1; j++)
- {
- swapedCol[j + 1] = matrix[j][rowOrCol];
- }
- swapedCol[0] = bottomElement;
- for (int i = 0; i < rows; i++)
- {
- matrix[i][rowOrCol] = swapedCol[i];
- }
- }
- }
- public static void SwapedUp(int rows, int[][] matrix, int rowOrCol, int moves, int cols)
- {
- int[] swapedCol = new int[rows];
- for (int currSwap = 0; currSwap < moves % cols; currSwap++)
- {
- int topElement = matrix[0][rowOrCol];
- for (int j = 1; j < swapedCol.Length; j++)
- {
- swapedCol[j - 1] = matrix[j][rowOrCol];
- }
- swapedCol[swapedCol.Length - 1] = topElement;
- for (int i = 0; i < rows; i++)
- {
- matrix[i][rowOrCol] = swapedCol[i];
- }
- }
- }
- public static void SwapToRight(int cols, int[][] matrix, int rowOrCol, int moves, int rows)
- {
- int[] swapedRow = new int[cols];
- for (int currSwap = 0; currSwap < moves % rows; currSwap++)
- {
- int lastElement = matrix[rowOrCol][cols - 1];
- for (int j = 0; j < swapedRow.Length - 1; j++)
- {
- swapedRow[j + 1] = matrix[rowOrCol][j];
- }
- swapedRow[0] = lastElement;
- matrix[rowOrCol] = swapedRow;
- }
- }
- public static void SwapToLeft(int cols, int[][] matrix, int rowOrCol, int moves, int rows)
- {
- int[] swapedRow = new int[cols];
- for (int currSwap = 0; currSwap < moves % rows; currSwap++)
- {
- int firstElement = matrix[rowOrCol][0];
- for (int j = 1; j < swapedRow.Length; j++)
- {
- swapedRow[j - 1] = matrix[rowOrCol][j];
- }
- swapedRow[swapedRow.Length - 1] = firstElement;
- matrix[rowOrCol] = swapedRow;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement