Advertisement
Guest User

04.

a guest
Jan 19th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _04
  5. {
  6. class Program
  7. {
  8. public static string[,] matrix;
  9. public static bool inside;
  10. static void Main(string[] args)
  11. {
  12. var size = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  13.  
  14. var rows = size[0];
  15. var cols = size[1];
  16.  
  17. matrix = new string[rows, cols];
  18.  
  19. matrixRead(rows, cols);
  20.  
  21. inside = false;
  22.  
  23. while (true)
  24. {
  25. var command = Console.ReadLine();
  26. var cmd = command.Split(" ",StringSplitOptions.RemoveEmptyEntries);
  27. if (cmd[0] == "END")
  28. {
  29. break;
  30. }
  31. if(cmd[0].StartsWith("swap") && cmd.Length == 5)
  32. {
  33. var currentCordinatesRow = int.Parse(cmd[1]);
  34. var currentCordinatesCol = int.Parse(cmd[2]);
  35. var cordinatesToSwapRow = int.Parse(cmd[3]);
  36. var cordinatesToSwapCol = int.Parse(cmd[4]);
  37.  
  38. IsInside(currentCordinatesRow, currentCordinatesCol, cordinatesToSwapRow, cordinatesToSwapCol,matrix);
  39.  
  40. if(inside)
  41. {
  42. var currentNum = matrix[currentCordinatesRow, currentCordinatesCol];
  43. matrix[currentCordinatesRow, currentCordinatesCol] = matrix[cordinatesToSwapRow, cordinatesToSwapCol];
  44. matrix[cordinatesToSwapRow, cordinatesToSwapCol] = currentNum;
  45. PrintMatrix(matrix);
  46. }
  47. else
  48. {
  49. Console.WriteLine("Invalid input!");
  50. }
  51. }
  52. else
  53. {
  54. Console.WriteLine("Invalid input!");
  55. }
  56. }
  57.  
  58. }
  59.  
  60. private static void PrintMatrix(string[,] matrix)
  61. {
  62. for (int row = 0; row < matrix.GetLength(0); row++)
  63. {
  64. for (int col = 0; col < matrix.GetLength(1); col++)
  65. {
  66. Console.Write(matrix[row, col] + " ");
  67. }
  68. Console.WriteLine();
  69. }
  70. }
  71.  
  72. private static void matrixRead(int rows, int cols)
  73. {
  74. for (int row = 0; row < matrix.GetLength(0) ; row++)
  75. {
  76. var currentRow = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries);
  77. for (int col = 0; col < matrix.GetLength(1); col++)
  78. {
  79. matrix[row, col] = currentRow[col];
  80. }
  81. }
  82. }
  83.  
  84. private static void IsInside(int currentCordinatesRow, int currentCordinatesCol, int cordinatesToSwapRow, int cordinatesToSwapCol, string[,] matrix)
  85. {
  86. var matrixCol = matrix.GetLength(1);
  87. var matrixRow = matrix.GetLength(0);
  88. if(currentCordinatesRow <= matrixRow && currentCordinatesCol <= matrixCol && cordinatesToSwapRow <= matrixRow && cordinatesToSwapCol <= matrixCol)
  89. {
  90. inside = true;
  91. }
  92. else
  93. {
  94. inside = false;
  95. }
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement