Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <utility>
- class Index
- {
- int x, y, x_max, y_max;
- public:
- 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) {}
- void moveIndex();
- int getX()
- {
- return x;
- }
- int getY()
- {
- return y;
- }
- };
- void Index::moveIndex()
- {
- if( x == 0 )
- {
- if ( y < y_max )
- {
- y++;
- return;
- }
- else if ( y == y_max )
- {
- x++;
- return;
- }
- }
- else if( x == x_max )
- {
- if ( y > 0 )
- {
- y--;
- return;
- }
- if ( y == 0 )
- {
- x--;
- return;
- }
- }
- else
- {
- if ( y == y_max )
- {
- x++;
- return;
- }
- if ( y == 0 )
- {
- x--;
- return;
- }
- }
- }
- std::vector<std::vector<int>> createVector(int rows, int columns)
- {
- std::vector<std::vector<int>> v;
- for(int i = 0; i < rows; i++)
- v.push_back( std::vector<int>(columns) );
- return v;
- }
- void writeArray(std::vector<std::vector<int>> arr)
- {
- for(auto x : arr)
- {
- for(auto y : x)
- std::cout << y << " ";
- std::cout << "\n";
- }
- }
- void rotateArray(std::vector<std::vector<int>> &arr, int rows, int columns)
- {
- int steps = 2 * (rows-1) + (2 * (columns-1)) - 1;
- Index a(0, 0, arr.size(), arr[0].size()), b(0, 1, arr.size(), arr[0].size());
- for(int i = 0; i < steps; i++)
- {
- std::swap( arr[a.getX()][a.getY()], arr[b.getX()][b.getY()] );
- a.moveIndex();
- b.moveIndex();
- }
- }
- int main()
- {
- int t, l, k;
- std::cin>>t;
- for(int i = 0; i < t; i++)
- {
- std::cin>>l>>k;
- auto arr = createVector(l, k);
- for(int j = 0; j < l; j++)
- for(int h = 0; h < k; h++)
- std::cin>>arr[j][h];
- rotateArray(arr, l, k);
- writeArray(arr);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement