Guest User

Untitled

a guest
Mar 20th, 2017
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. namespace RubiksMatrix
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. public class Program
  7. {
  8. private static int[][] matrix = new int[1][];
  9. private static int rows = 0;
  10. private static int cols = 0;
  11.  
  12. private static void InitMatrix(int rows, int cols)
  13. {
  14. matrix = new int[rows][];
  15. for (int i = 0; i < rows; i++)
  16. {
  17. matrix[i] = new int[cols];
  18. for (int j = 0; j < cols; j++)
  19. {
  20. matrix[i][j] = i * cols + (j + 1);
  21. }
  22. }
  23. }
  24.  
  25. private static void moveUpOrDown(int line, long moves, string direction)
  26. {
  27. int m = (int)(moves % cols);
  28. var colValues = new Queue<int>();
  29.  
  30. if (direction == "down")
  31. {
  32. for (int i = rows - 1; i >= 0; i--)
  33. {
  34. colValues.Enqueue(matrix[i][line]);
  35. }
  36. }
  37. else
  38. {
  39. for (int i = 0; i < rows; i++)
  40. {
  41. colValues.Enqueue(matrix[i][line]);
  42. }
  43. }
  44. for (int i = 0; i < m; i++)
  45. {
  46. int t = colValues.Dequeue();
  47. colValues.Enqueue(t);
  48. }
  49. if(direction == "down")
  50. {
  51. for (int i = rows - 1; i >= 0; i--)
  52. {
  53. matrix[i][line] = colValues.Dequeue();
  54. }
  55. }
  56. else
  57. {
  58. for (int i = 0; i < rows; i++)
  59. {
  60. matrix[i][line] = colValues.Dequeue();
  61. }
  62. }
  63. }
  64.  
  65. private static void moveLeftOrRight(int line, long moves, string direction)
  66. {
  67. int m = (int)(moves % cols);
  68. var colValues = new Queue<int>();
  69.  
  70. if (direction == "right")
  71. {
  72. for (int i = cols - 1; i >= 0; i--)
  73. {
  74. colValues.Enqueue(matrix[line][i]);
  75. }
  76. }
  77. else
  78. {
  79. for (int i = 0; i < cols; i++)
  80. {
  81. colValues.Enqueue(matrix[line][i]);
  82. }
  83. }
  84. for (int i = 0; i < m; i++)
  85. {
  86. int t = colValues.Dequeue();
  87. colValues.Enqueue(t);
  88. }
  89. if (direction == "right")
  90. {
  91. for (int i = cols - 1; i >= 0; i--)
  92. {
  93. matrix[line][i] = colValues.Dequeue();
  94. }
  95. }
  96. else
  97. {
  98. for (int i = 0; i < cols; i++)
  99. {
  100. matrix[line][i] = colValues.Dequeue();
  101. }
  102. }
  103. }
  104.  
  105. public static void RearrangeMatrix()
  106. {
  107. for (int i = 0; i < rows; i++)
  108. {
  109. for (int j = 0; j < cols; j++)
  110. {
  111. int initValue = i * cols + (j + 1);
  112. int currentValue = matrix[i][j];
  113.  
  114. if (initValue != currentValue)
  115. {
  116. for (int k = 0; k < rows; k++)
  117. {
  118. var currentRow = matrix[k];
  119. int index = Array.IndexOf(currentRow, initValue);
  120. if (index > -1)
  121. {
  122. matrix[k][index] = currentValue;
  123. matrix[i][j] = initValue;
  124. Console.WriteLine($"Swap ({i}, {j}) with ({k}, {index})");
  125. break;
  126. }
  127. }
  128. }
  129. else
  130. {
  131. Console.WriteLine("No swap required");
  132. }
  133. }
  134. }
  135. }
  136.  
  137. public static void Main(string[] args)
  138. {
  139. var matrixDimensions = Console.ReadLine()
  140. .Split(' ')
  141. .Select(int.Parse)
  142. .ToArray();
  143. rows = matrixDimensions[0];
  144. cols = matrixDimensions[1];
  145. InitMatrix(rows, cols);
  146.  
  147. int n = int.Parse(Console.ReadLine());
  148. long moves = 0;
  149. string direction;
  150. int lineNumber;
  151. for (int i = 0; i < n; i++)
  152. {
  153. var command = Console.ReadLine()
  154. .Split(' ')
  155. .ToArray();
  156. lineNumber = int.Parse(command[0]);
  157. direction = command[1];
  158. moves = long.Parse(command[2]);
  159.  
  160. switch (direction)
  161. {
  162. case "up":
  163. case "down":
  164. moveUpOrDown(lineNumber, moves, direction);
  165. break;
  166. case "left":
  167. case "right":
  168. moveLeftOrRight(lineNumber, moves, direction);
  169. break;
  170. }
  171. }
  172. RearrangeMatrix();
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment