Advertisement
Guest User

Rubiks Matrix

a guest
Feb 1st, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class RubiksMatrix
  5. {
  6. public static void Main()
  7. {
  8. int[] dimensions = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  9. int n = int.Parse(Console.ReadLine());
  10.  
  11. int rows = dimensions[0];
  12. int cols = dimensions[1];
  13. int[][] matrix = new int[rows][];
  14.  
  15. FillMatrix(matrix, rows, cols);
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. string[] commandAgs = Console.ReadLine().Split(' ');
  20.  
  21. int rowOrCol = int.Parse(commandAgs[0]);
  22. string direction = commandAgs[1];
  23. int moves = int.Parse(commandAgs[2]);
  24.  
  25. if (direction == "left")
  26. {
  27. SwapToLeft(cols, matrix, rowOrCol, moves, rows);
  28. }
  29. else if (direction == "right")
  30. {
  31. SwapToRight(cols, matrix, rowOrCol, moves, rows);
  32. }
  33. else if (direction == "up")
  34. {
  35. SwapedUp(rows, matrix, rowOrCol, moves, cols);
  36. }
  37. else if (direction == "down")
  38. {
  39. SwapDown(rows, matrix, rowOrCol, moves, cols);
  40. }
  41. }
  42.  
  43. int m = 1;
  44.  
  45. for (int currRow = 0; currRow < rows; currRow++)
  46. {
  47. for (int currCol = 0; currCol < cols; currCol++)
  48. {
  49. if (matrix[currRow][currCol] == currRow + currCol + m)
  50. {
  51. Console.WriteLine("No swap required");
  52. }
  53. else
  54. {
  55. int currPosValue = matrix[currRow][currCol];
  56. int currPosRow = currRow;
  57. int currPosCol = currCol;
  58. int nextPosRow = 0;
  59. int nextPosCol = 0;
  60. int nextPosValue = 0;
  61.  
  62. for (int r = 0; r < rows; r++)
  63. {
  64. for (int c = 0; c < cols; c++)
  65. {
  66. if ((matrix[r][c] == currRow + currCol + m) && (matrix[currRow][currCol] != currRow + currCol + m))
  67. {
  68. nextPosRow = r;
  69. nextPosCol = c;
  70. nextPosValue = matrix[r][c];
  71. matrix[currPosRow][currPosCol] = nextPosValue;
  72. matrix[nextPosRow][nextPosCol] = currPosValue;
  73. break;
  74. }
  75. }
  76. }
  77.  
  78. Console.WriteLine($"Swap ({currPosRow}, {currPosCol}) with ({nextPosRow}, {nextPosCol})");
  79. }
  80. }
  81.  
  82. m += 2;
  83. }
  84. }
  85.  
  86. public static void FillMatrix(int[][] matrix, int rows, int cols)
  87. {
  88. int count = 1;
  89.  
  90. for (int row = 0; row < rows; row++)
  91. {
  92. matrix[row] = new int[cols];
  93.  
  94. for (int col = 0; col < cols; col++)
  95. {
  96. matrix[row][col] = count;
  97. count++;
  98. }
  99. }
  100. }
  101.  
  102. public static void SwapDown(int rows, int[][] matrix, int rowOrCol, int moves, int cols)
  103. {
  104. int[] swapedCol = new int[rows];
  105.  
  106. for (int currSwap = 0; currSwap < moves % cols; currSwap++)
  107. {
  108. int bottomElement = matrix[rows - 1][rowOrCol];
  109.  
  110. for (int j = 0; j < swapedCol.Length - 1; j++)
  111. {
  112. swapedCol[j + 1] = matrix[j][rowOrCol];
  113. }
  114.  
  115. swapedCol[0] = bottomElement;
  116.  
  117. for (int i = 0; i < rows; i++)
  118. {
  119. matrix[i][rowOrCol] = swapedCol[i];
  120. }
  121. }
  122. }
  123.  
  124. public static void SwapedUp(int rows, int[][] matrix, int rowOrCol, int moves, int cols)
  125. {
  126. int[] swapedCol = new int[rows];
  127.  
  128. for (int currSwap = 0; currSwap < moves % cols; currSwap++)
  129. {
  130. int topElement = matrix[0][rowOrCol];
  131.  
  132. for (int j = 1; j < swapedCol.Length; j++)
  133. {
  134. swapedCol[j - 1] = matrix[j][rowOrCol];
  135. }
  136.  
  137. swapedCol[swapedCol.Length - 1] = topElement;
  138.  
  139. for (int i = 0; i < rows; i++)
  140. {
  141. matrix[i][rowOrCol] = swapedCol[i];
  142. }
  143. }
  144. }
  145.  
  146. public static void SwapToRight(int cols, int[][] matrix, int rowOrCol, int moves, int rows)
  147. {
  148. int[] swapedRow = new int[cols];
  149.  
  150. for (int currSwap = 0; currSwap < moves % rows; currSwap++)
  151. {
  152. int lastElement = matrix[rowOrCol][cols - 1];
  153.  
  154. for (int j = 0; j < swapedRow.Length - 1; j++)
  155. {
  156. swapedRow[j + 1] = matrix[rowOrCol][j];
  157. }
  158.  
  159. swapedRow[0] = lastElement;
  160. matrix[rowOrCol] = swapedRow;
  161. }
  162. }
  163.  
  164. public static void SwapToLeft(int cols, int[][] matrix, int rowOrCol, int moves, int rows)
  165. {
  166. int[] swapedRow = new int[cols];
  167.  
  168. for (int currSwap = 0; currSwap < moves % rows; currSwap++)
  169. {
  170. int firstElement = matrix[rowOrCol][0];
  171.  
  172. for (int j = 1; j < swapedRow.Length; j++)
  173. {
  174. swapedRow[j - 1] = matrix[rowOrCol][j];
  175. }
  176.  
  177. swapedRow[swapedRow.Length - 1] = firstElement;
  178. matrix[rowOrCol] = swapedRow;
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement