Advertisement
svetlyoek

Untitled

May 22nd, 2019
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 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" && tokens.Length == 5)
  33. {
  34.  
  35. int firstRow = int.Parse(tokens[1]);
  36. int firstCol = int.Parse(tokens[2]);
  37. int secondRow = int.Parse(tokens[3]);
  38. int secondCol = int.Parse(tokens[4]);
  39.  
  40. if (firstRow >= 0 && firstRow <= rows - 1 && firstCol >= 0 && firstCol <= cols - 1
  41. && secondRow >= 0 && secondRow <= rows - 1 && secondCol >= 0 && secondCol <= cols - 1)
  42. {
  43.  
  44. var temp = arr[firstRow, firstCol];
  45. arr[firstRow, firstCol] = arr[secondRow, secondCol];
  46. arr[secondRow, secondCol] = temp;
  47.  
  48. for (int i = 0; i < arr.GetLength(0); i++)
  49. {
  50. for (int j = 0; j < arr.GetLength(1); j++)
  51. {
  52. Console.Write($"{arr[i, j]} ");
  53. }
  54. Console.WriteLine();
  55.  
  56. }
  57. }
  58. else
  59. {
  60. Console.WriteLine($"Invalid input!");
  61. }
  62. }
  63.  
  64. else
  65. {
  66. Console.WriteLine($"Invalid input!");
  67. }
  68. }
  69. }
  70.  
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement