Advertisement
desislava_topuzakova

4.

May 19th, 2022
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MatrixShuffling
  4. {
  5. internal class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. string input = Console.ReadLine(); //"3 4".Split() -> ["3", "4"]
  10. int rows = int.Parse(input.Split()[0]); //бр. редове
  11. int cols = int.Parse(input.Split()[1]); //бр. колони
  12.  
  13. string[,] matrix = new string[rows, cols];
  14. FillMatrix(matrix);
  15.  
  16. string command = Console.ReadLine();
  17.  
  18. while (command != "END")
  19. {
  20. //"swap row1 col1 row2 col2"
  21. //1. проверка дали командата е валидна
  22. //2. проверка дали редове или колоните
  23. if (!ValidateCommand(command, rows, cols))
  24. {
  25. Console.WriteLine("Invalid input!");
  26. command = Console.ReadLine();
  27. continue;
  28. }
  29. else
  30. {
  31. //валидна команда -> "swap 1 3 4 2"
  32. string[] commandParts = command.Split(); //["swap", "1", "3", "4", "2"]
  33. int row1 = int.Parse(commandParts[1]); //ред на първия елемент
  34. int col1 = int.Parse(commandParts[2]); //колонана първия елемент
  35. int row2 = int.Parse(commandParts[3]); //ред на втория елемент
  36. int col2 = int.Parse(commandParts[4]);//колона на втория елемент
  37. //1. елемента на row1, col1
  38. string firstElement = matrix[row1, col1];
  39. //2. елемента на row2, col2
  40. string secondElement = matrix[row2, col2];
  41. //3. размяна
  42. //първи -> row2, col2
  43. matrix[row2, col2] = firstElement;
  44. //втори -> row1, col1
  45. matrix[row1, col1] = secondElement;
  46.  
  47. PrintMatrix(matrix);
  48. }
  49.  
  50.  
  51. command = Console.ReadLine();
  52. }
  53. }
  54.  
  55. private static void PrintMatrix(string[,] matrix)
  56. {
  57. for (int row = 0; row < matrix.GetLength(0); row++)
  58. {
  59. for (int col = 0; col < matrix.GetLength(1); col++)
  60. {
  61. Console.Write(matrix[row, col] + " ");
  62. }
  63. Console.WriteLine();
  64. }
  65. }
  66.  
  67. //true -> ако командата е валидна
  68. //false -> ако командата не е валидна
  69. private static bool ValidateCommand(string command, int rows, int cols)
  70. {
  71. //"swap 1 3 4 2"
  72. string[] commandParts = command.Split(); //["swap", "1", "3", "4", "2"]
  73. //1. първата дума е swap
  74. //2. командата има точно 5 части
  75. if (commandParts[0] == "swap" && commandParts.Length == 5)
  76. {
  77. //валидна команда -> проверка за редове и колони
  78. int row1 = int.Parse(commandParts[1]);
  79. int col1 = int.Parse(commandParts[2]);
  80. int row2 = int.Parse(commandParts[3]);
  81. int col2 = int.Parse(commandParts[4]);
  82.  
  83. if (row1 >= 0 && row1 < rows
  84. && col1 >= 0 && col1 < cols
  85. && row2 >= 0 && row2 < rows
  86. && col2 >= 0 && col2 < cols)
  87. {
  88. return true;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94.  
  95. }
  96. else
  97. {
  98. return false;
  99. }
  100. }
  101.  
  102. private static void FillMatrix(string[,] matrix)
  103. {
  104. for (int row = 0; row < matrix.GetLength(0); row++)
  105. {
  106. string[] rowData = Console.ReadLine().Split(); //"A B B D".Split() -> ["A", "B", "B", "D"]
  107. for (int col = 0; col < matrix.GetLength(1); col++)
  108. {
  109. matrix[row, col] = rowData[col];
  110. }
  111. }
  112. }
  113. }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement