Advertisement
svetlyoek

Untitled

May 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Matrix_shuffling
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] rowsAndCols = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  11. int rows = rowsAndCols[0];
  12. int cols = rowsAndCols[1];
  13. string[,] arr = new string[rows, cols];
  14.  
  15. for (int row = 0; row < rows; row++)
  16. {
  17. string[] lines = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
  18.  
  19. for (int col = 0; col < cols; col++)
  20. {
  21. arr[row, col] = lines[col];
  22. }
  23. }
  24.  
  25. string commands = string.Empty;
  26.  
  27. while ((commands = Console.ReadLine()) != "END")
  28. {
  29. string[] tokens = commands.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();
  30.  
  31.  
  32. if (tokens[0] == "swap")
  33. {
  34. int firstRow = int.Parse(tokens[1]);
  35. int firstCol = int.Parse(tokens[2]);
  36. int secondRow = int.Parse(tokens[3]);
  37. int secondCol = int.Parse(tokens[4]);
  38.  
  39. if (firstRow >= 0 && firstRow <= rows - 1 && firstCol >= 0 && firstCol <= cols - 1
  40. && secondRow >= 0 && secondRow <= rows - 1 && secondCol >= 0 && secondCol <= cols - 1)
  41. {
  42.  
  43. var temp = arr[firstRow, firstCol];
  44. arr[firstRow, firstCol] = arr[secondRow, secondCol];
  45. arr[secondRow, secondCol] = temp;
  46.  
  47. for (int i = 0; i < arr.GetLength(0); i++)
  48. {
  49. for (int j = 0; j < arr.GetLength(1); j++)
  50. {
  51. Console.Write($"{arr[i, j]} ");
  52. }
  53. Console.WriteLine();
  54.  
  55. }
  56. }
  57. else
  58. {
  59. Console.WriteLine($"Invalid input!");
  60. }
  61. }
  62. else
  63. {
  64. Console.WriteLine($"Invalid input!");
  65. }
  66. }
  67.  
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement