Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. class MatrixShuffling
  5. {
  6. static void Main()
  7. {
  8. //Read the matrix dimentions from the console
  9. var dim = Console.ReadLine()
  10. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  11. .Select(long.Parse)
  12. .ToArray();
  13. var n = dim[0];
  14. var m = dim[1];
  15. var matrix = new string[n, m];
  16.  
  17. //Fill the matrix
  18. for (int i = 0; i < matrix.GetLength(0); i++)
  19. {
  20. var a = Console.ReadLine()
  21. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  22. .ToArray();
  23.  
  24. for (int j = 0; j < matrix.GetLength(1); j++)
  25. {
  26. matrix[i, j] = a[j];
  27. }
  28. }
  29.  
  30. var commands = new string[5];
  31. while (true)
  32. {
  33. commands = Console.ReadLine()
  34. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  35. .ToArray();
  36. if (commands[0] == "swap")
  37. {
  38. var y1 = long.Parse(commands[1]);
  39. var x1 = long.Parse(commands[2]);
  40. var y2 = long.Parse(commands[3]);
  41. var x2 = long.Parse(commands[4]);
  42.  
  43. if (!AreDimentionsValid(y1, x1, y2, x2, n, m))
  44. {
  45. Console.WriteLine("Invalid input!");
  46. continue;
  47. }
  48.  
  49. //Swapping algorithm
  50. string tempValue = matrix[y1, x1];
  51. matrix[y1, x1] = matrix[y2, x2];
  52. matrix[y2, x2] = tempValue;
  53.  
  54. //Call printing method
  55. PrintMatrix(matrix);
  56. }
  57. else if (commands[0] == "END")
  58. {
  59. break;
  60. }
  61. else
  62. {
  63. Console.WriteLine("Invalid input!");
  64. continue;
  65. }
  66. }
  67. }
  68.  
  69. static bool AreDimentionsValid(long y1, long x1, long y2, long x2,
  70. long rowLength, long colLength)
  71. {
  72. bool areValid = true;
  73.  
  74. if (x1 < 0 || x1 >= colLength)
  75. {
  76. areValid = false;
  77. }
  78. else if (x2 < 0 || x2 >= colLength)
  79. {
  80. areValid = false;
  81. }
  82. else if (y1 < 0 || y1 >= rowLength)
  83. {
  84. areValid = false;
  85. }
  86. else if (y2 < 0 || y2 >= rowLength)
  87. {
  88. areValid = false;
  89. }
  90.  
  91. return areValid;
  92. }
  93.  
  94. static void PrintMatrix(string[,] matrix)
  95. {
  96. for (int i = 0; i < matrix.GetLength(0); i++)
  97. {
  98. for (int j = 0; j < matrix.GetLength(1); j++)
  99. {
  100. Console.Write(matrix[i, j] + " ");
  101. }
  102. Console.WriteLine();
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement