Advertisement
TimmyChannel

4

Apr 16th, 2024
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main()
  6. {
  7. var inputString = Console.ReadLine().Split();
  8. int n = int.Parse(inputString[0]);
  9. char direction = inputString[1].First();
  10. long[,] matrix = new long[n, n];
  11. for (int i = 0; i < n; i++)
  12. {
  13. string[] row = Console.ReadLine().Split();
  14. for (int j = 0; j < n; j++)
  15. {
  16. matrix[i, j] = long.Parse(row[j]);
  17. }
  18. }
  19. if (direction == 'L')
  20. RotateMatrixLeft(matrix, n);
  21. else
  22. RotateMatrixRight(matrix, n);
  23. }
  24. static void RotateMatrixRight(long[,] matrix, int n)
  25. {
  26. var operationList = new List<string>();
  27.  
  28.     for (int i = 0; i < n / 2; i++)
  29.     {
  30.         for (int j = i; j < n - i - 1; j++)
  31.         {
  32.             int x1 = i, y1 = j;
  33.             int x2 = j, y2 = n - 1 - i;
  34.             int x3 = n - 1 - i, y3 = n - 1 - j;
  35.             int x4 = n - 1 - j, y4 = i;
  36.  
  37.             operationList.Add(Swap(matrix, x1, y1, x2, y2));
  38.             operationList.Add(Swap(matrix, x2, y2, x3, y3));
  39.             operationList.Add(Swap(matrix, x3, y3, x4, y4));
  40.         }
  41.     }
  42.  
  43.     Console.WriteLine(operationList.Count);
  44.     foreach (var op in operationList)
  45.     {
  46.         Console.WriteLine(op);
  47.     }
  48. }
  49.  
  50. static void RotateMatrixLeft(long[,] matrix, int n)
  51. {
  52.     var operationList = new List<string>();
  53.     for (int i = 0; i < n / 2; i++)
  54.     {
  55.         for (int j = i; j < n - i - 1; j++)
  56.         {
  57.             int x1 = i, y1 = j;
  58.             int x2 = j, y2 = n - 1 - i;
  59.             int x3 = n - 1 - i, y3 = n - 1 - j;
  60.             int x4 = n - 1 - j, y4 = i;
  61.  
  62.             operationList.Add(Swap(matrix, x1, y1, x4, y4));
  63.             operationList.Add(Swap(matrix, x4, y4, x3, y3));
  64.             operationList.Add(Swap(matrix, x3, y3, x2, y2));
  65.  
  66.         }
  67.     }
  68.  
  69.     Console.WriteLine(operationList.Count);
  70.     foreach (var op in operationList)
  71.     {
  72.         Console.WriteLine(op);
  73.     }
  74. }
  75.  
  76. static string Swap(long[,] matrix, int x1, int y1, int x2, int y2)
  77. {
  78.     (matrix[x1, y1], matrix[x2, y2]) = (matrix[x2, y2], matrix[x1, y1]);
  79.     return $"{x1} {y1} {x2} {y2}";
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement