Advertisement
Glenpl

Untitled

Jun 29th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4.  
  5. class Index
  6. {
  7.     int x, y, x_max, y_max;
  8.  
  9. public:
  10.     Index(int x, int y, int x_max, int y_max) : x(x), y(y), x_max(x_max-1), y_max(y_max-1) {}
  11.  
  12.     void moveIndex();
  13.     int getX()
  14.     {
  15.         return x;
  16.     }
  17.     int getY()
  18.     {
  19.         return y;
  20.     }
  21. };
  22.  
  23. void Index::moveIndex()
  24. {
  25.     if( x == 0 )
  26.     {
  27.         if ( y < y_max )
  28.         {
  29.             y++;
  30.             return;
  31.         }
  32.  
  33.         else if ( y == y_max )
  34.         {
  35.             x++;
  36.             return;
  37.         }
  38.     }
  39.  
  40.     else if( x == x_max )
  41.     {
  42.         if ( y > 0 )
  43.         {
  44.             y--;
  45.             return;
  46.         }
  47.  
  48.         if ( y == 0 )
  49.         {
  50.             x--;
  51.             return;
  52.         }
  53.     }
  54.  
  55.     else
  56.     {
  57.         if ( y == y_max )
  58.         {
  59.             x++;
  60.             return;
  61.         }
  62.         if ( y == 0 )
  63.         {
  64.             x--;
  65.             return;
  66.         }
  67.     }
  68. }
  69.  
  70. std::vector<std::vector<int>> createVector(int rows, int columns)
  71. {
  72.     std::vector<std::vector<int>> v;
  73.     for(int i = 0; i < rows; i++)
  74.         v.push_back( std::vector<int>(columns) );
  75.     return v;
  76. }
  77.  
  78. void writeArray(std::vector<std::vector<int>> arr)
  79. {
  80.     for(auto x : arr)
  81.     {
  82.         for(auto y : x)
  83.             std::cout << y << " ";
  84.         std::cout << "\n";
  85.     }
  86. }
  87.  
  88. void rotateArray(std::vector<std::vector<int>> &arr, int rows, int columns)
  89. {
  90.     int steps = 2 * (rows-1) + (2 * (columns-1)) - 1;
  91.     Index a(0, 0, arr.size(), arr[0].size()), b(0, 1, arr.size(), arr[0].size());
  92.     for(int i = 0; i < steps; i++)
  93.     {
  94.         std::swap( arr[a.getX()][a.getY()], arr[b.getX()][b.getY()]  );
  95.         a.moveIndex();
  96.         b.moveIndex();
  97.     }
  98. }
  99.  
  100. int main()
  101. {
  102.     int t, l, k;
  103.     std::cin>>t;
  104.     for(int i = 0; i < t; i++)
  105.     {
  106.         std::cin>>l>>k;
  107.         auto arr = createVector(l, k);
  108.         for(int j = 0; j < l; j++)
  109.             for(int h = 0; h < k; h++)
  110.                 std::cin>>arr[j][h];
  111.  
  112.         rotateArray(arr, l, k);
  113.         writeArray(arr);
  114.     }
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement