Advertisement
Bubeto

Untitled

Jan 22nd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. namespace Matrix_Shuffling
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] dimentions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11. int rows = dimentions[0];
  12. int cols = dimentions[1];
  13. string[,] matrix = new string[rows, cols];
  14.  
  15. for (int row = 0; row < rows; row++)
  16. {
  17. string[] currentRow = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  18. for (int col = 0; col < cols; col++)
  19. {
  20. matrix[row, col] = currentRow[col];
  21. }
  22.  
  23. }
  24. string[] command = Console.ReadLine().ToLower().Split().ToArray();
  25.  
  26. while (command[0] != "END")
  27. {
  28.  
  29. if (command[0] == "swap" && command.Count() == 5)
  30. {
  31. int[] cells = command.Skip(1).Select(int.Parse).ToArray();
  32.  
  33. if ( cells[0] >= 0 && cells[0] < dimentions[0]
  34. && cells[1] >= 0 && cells[1] < dimentions[1]
  35. && cells[2] >= 0 && cells[2] < dimentions[0]
  36. && cells[3] >= 0 && cells[3] < dimentions[1])
  37. {
  38. string firstCell = matrix[cells[0], cells[1]];
  39. matrix[cells[0], cells[1]] = matrix[cells[2], cells[3]];
  40. matrix[cells[2], cells[3]] = firstCell;
  41.  
  42. StringBuilder sb = new StringBuilder();
  43.  
  44. for (int i = 0; i < matrix.GetLength(0); i++)
  45. {
  46. for (int j = 0; j < matrix.GetLength(1); j++)
  47. {
  48. sb.Append(matrix[i, j] + " ");
  49. }
  50.  
  51. if (i != matrix.GetLength(0) - 1)
  52. {
  53. sb.AppendLine();
  54. }
  55.  
  56. }
  57.  
  58. Console.WriteLine(sb);
  59. }
  60. else
  61. {
  62. Console.WriteLine("Invalid input!");
  63. }
  64. }
  65.  
  66. else
  67. {
  68. Console.WriteLine("Invalid input!");
  69. }
  70.  
  71. command = Console.ReadLine().Split();
  72. }
  73. }
  74.  
  75.  
  76.  
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement