Guest User

Untitled

a guest
Jan 29th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace matrixCubes
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. var indexes = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  11. .Select(int.Parse)
  12. .ToArray();
  13.  
  14. var matrice = MatriceCreator(indexes);
  15.  
  16. int numOperations = int.Parse(Console.ReadLine());
  17.  
  18. for (int i = 0; i < numOperations; i++)
  19. {
  20. var commands = Console.ReadLine()
  21. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  22. .ToArray();
  23.  
  24. OperationManager(commands, matrice);
  25.  
  26. }
  27.  
  28. ReArrangeManager(matrice);
  29. }
  30.  
  31. private static void ReArrangeManager(int[,] matrice)
  32. {
  33.  
  34. for (int row = 0; row < matrice.GetLength(0); row++)
  35. {
  36. for (int col = 0; col < matrice.GetLength(1); col++)
  37. {
  38. var properValuee = row * matrice.GetLength(1) + col + 1;
  39. if (matrice[row, col] == properValuee)
  40. {
  41. Console.WriteLine("No swap required");
  42. }
  43. else
  44. {
  45. var position = FindValuePosition(properValuee, matrice);
  46. var currentPosition = new int[2];
  47.  
  48. currentPosition[0] = row;
  49. currentPosition[1] = col;
  50.  
  51. Console.WriteLine($"Swap ({row}, {col}) with ({position[0]}, {position[1]})");
  52. SwapValues(matrice, currentPosition, position);
  53. }
  54. }
  55. }
  56.  
  57.  
  58. }
  59.  
  60. private static void SwapValues(int[,] matrice, int[] currentPosition, int[] position)
  61. {
  62. var tempValue = matrice[currentPosition[0], currentPosition[1]];
  63.  
  64. matrice[currentPosition[0], currentPosition[1]] = matrice[position[0], position[1]];
  65. matrice[position[0], position[1]] = tempValue;
  66.  
  67.  
  68. }
  69.  
  70. private static int[] FindValuePosition(int properValuee, int[,] matrice)
  71. {
  72.  
  73. for (int row = 0; row < matrice.GetLength(0); row++)
  74. {
  75. for (int col = 0; col < matrice.GetLength(1); col++)
  76. {
  77. if (matrice[row, col]==properValuee)
  78. {
  79. var indexes = new int[2];
  80. indexes[0] = row;
  81. indexes[1] = col;
  82. return indexes;
  83. }
  84. }
  85. }
  86. return null ;
  87. }
  88.  
  89. private static void OperationManager(string[] commands, int[,] matrice)
  90. {
  91. var direction = commands[1];
  92.  
  93. switch (direction)
  94. {
  95. case "right": MoveRight(commands, matrice);
  96. break;
  97. case "left":
  98. MoveLeft(commands, matrice);
  99. break;
  100. case "up":
  101. MoveUp(commands, matrice);
  102. break;
  103. case "down":
  104. MoveDown(commands, matrice);
  105. break;
  106. }
  107.  
  108.  
  109.  
  110. }
  111.  
  112. private static void MoveDown(string[] commands, int[,] matrice)
  113. {
  114. int moves = int.Parse(commands[2]);
  115.  
  116. int col = int.Parse(commands[0]);
  117. // the tempArray holds the numbers after the moves
  118. var tempArray = new int[matrice.GetLength(0)];
  119.  
  120. for (int row = 0; row < matrice.GetLength(0); row++)
  121. {
  122. tempArray[row] = matrice[row, col];
  123. }
  124.  
  125. for (int z = 0; z < moves; z++)
  126. {
  127. var last = tempArray[tempArray.Length - 1];
  128.  
  129. for (int i = tempArray.Length-1; i > 0; i--)
  130. {
  131. tempArray[i] = tempArray[i-1];
  132. }
  133. tempArray[0] = last;
  134. }
  135.  
  136. //now we need to etract the numbers from the tempArray and put them into the matrix
  137. for (int row = 0; row < matrice.GetLength(0); row++)
  138. {
  139. matrice[row, col] = tempArray[row];
  140. }
  141.  
  142. }
  143.  
  144. private static void MoveUp(string[] commands, int[,] matrice)
  145. {
  146. int moves = int.Parse(commands[2]);
  147.  
  148. int col = int.Parse(commands[0]);
  149. // the tempArray holds the numbers after the moves
  150. var tempArray = new int[matrice.GetLength(0)];
  151.  
  152. for (int row = 0; row < matrice.GetLength(0); row++)
  153. {
  154. tempArray[row] = matrice[row, col];
  155. }
  156.  
  157. //here we shaffle the temp array according to the number of moves
  158. for (int z = 0; z < moves; z++)
  159. {
  160. var first = tempArray[0];
  161.  
  162. for (int i = 0; i < tempArray.Length -1; i++)
  163. {
  164. tempArray[i] = tempArray[i + 1];
  165. }
  166. tempArray[tempArray.Length - 1] = first;
  167.  
  168. }
  169.  
  170. //now we need to etract the numbers from the tempArray and put them into the matrix
  171. for (int row = 0; row < matrice.GetLength(0); row++)
  172. {
  173. matrice[row, col] = tempArray[row];
  174. }
  175.  
  176. }
  177.  
  178. private static void MoveLeft(string[] commands, int[,] matrice)
  179. {
  180. int moves = int.Parse(commands[2]);
  181. int row = int.Parse(commands[0]);
  182.  
  183. int[] tempArray = new int[matrice.GetLongLength(0)];
  184. //here we get the the row from the matrice
  185. for (int col = 0; col < matrice.GetLength(1); col++)
  186. {
  187. tempArray[col] = matrice[row, col];
  188. }
  189. //here we need to shuffle the numbers into the temp array N times pointin moves
  190.  
  191. for (int i = 0; i < moves; i++)
  192. {
  193. //logic for moving the numbers left <----
  194. int first = tempArray[0];
  195. /*here we need to access only the index before the last so we wont get out of the array boundaries*/
  196. for (int z = 0; z < tempArray.Length-1; z++)
  197. {
  198. tempArray[z] = tempArray[z + 1];
  199. }
  200. tempArray[tempArray.Length - 1] = first;
  201. }
  202. //after we are ready with the shuffle we need to pass the shaffled row into the matrix
  203. for (int col = 0; col < matrice.GetLongLength(1); col++)
  204. {
  205. matrice[row, col] = tempArray[col];
  206. }
  207. }
  208.  
  209. private static void MoveRight(string[] commands, int[,] matrice)
  210. {
  211. int moves = int.Parse(commands[2]);
  212. int row = int.Parse(commands[0]);
  213.  
  214. int[] tempArray = new int[matrice.GetLongLength(0)];
  215. //here we get the the row from the matrice
  216. for (int col = 0; col < matrice.GetLength(1); col++)
  217. {
  218. tempArray[col] = matrice[row, col];
  219. }
  220. //here we need to shuffle the numbers into the temp array N times pointin moves
  221.  
  222. for (int i = 0; i < moves; i++)
  223. {
  224. //logic for moving the numbers right ---->
  225. int last = tempArray[tempArray.Length-1];
  226. /*here we need to access only the index before the last so we wont get out of the array boundaries*/
  227. for (int z = tempArray.Length -1; z > 0; z--)
  228. {
  229. tempArray[z] = tempArray[z - 1];
  230. }
  231. tempArray[0] = last;
  232. }
  233. //after we are ready with the shuffle we need to pass the shaffled row into the matrix
  234. for (int col = 0; col < matrice.GetLongLength(1); col++)
  235. {
  236. matrice[row, col] = tempArray[col];
  237. }
  238. }
  239.  
  240. private static int[,] MatriceCreator(int[] indexes)
  241. {
  242. int rows = indexes[0];
  243. int columns = indexes[1];
  244.  
  245. var matrice = new int[rows, columns];
  246.  
  247. int counter = 1;
  248. for (int row = 0; row < matrice.GetLength(0); row++)
  249. {
  250. for (int column = 0; column < matrice.GetLength(1); column++)
  251. {
  252. matrice[row, column] = counter;
  253. counter++;
  254. }
  255. }
  256.  
  257. return matrice;
  258. }
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment