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;
- }
- }
- }
- /*
- 00 01 02
- 10 11 12
- 20 21 22
- 1 2 3
- 4 5 6
- 7 8 9
- 2 3 6
- 1 5 9
- 4 7 8
- 0 1 2
- 1 1 2
- 2 2 2
- 00 01 02
- 10 11 12
- 20 21 22
- 0,0 > 0,1 1
- 0,1 > 0,2 2
- 0,2 > 1,2 3
- 1,2 > 2,2 4
- 2,2 > 2,1 5
- 2,1 > 2,0 6
- 2,0 > 1,0 7
- for(int i = 0; i < x.size(); i++)
- for(int j = 0; j < x.size(); j++)
- 0 1 2 3
- 1 1 2 3
- 2 2 2 3
- 3 3 3 3
- 0,0 > 0,1 1
- 0,1 > 0,2 2
- 0,2 > 0,3 3
- 0,3 > 1,3 4
- 1,3 > 2,3 5
- 2,3 > 3,3 6
- 3,3 > 3,2 7
- 3,2 > 3,1 8
- 3,1 > 3,0 9
- 3,0 > 2,0 10
- 2,0 > 1,0 11
- 0 1 2 3 4
- 1 1 2 3 4
- 2 2 2 3 4
- 3 3 3 3 4
- 4 4 4 4 4
- kwadrat 5*5 = 4*4-1 kroków
- 0 1 2 3 4 5
- 1 1 2 3 4 5
- 2 2 2 3 4 5
- 3 3 3 3 4 5
- 4 4 4 4 4 5
- 17
- 5 * 6 - 17
- 6 + 12 - 1 = 18 - 1 = 17
- (bok - 2)
- |
- |
- |
- * 2
- + bok
- - - -
- * 2 - 1
- 2 * (wierszy - 2) + 2 * kolumny - 1
- 2 3 6
- 1 5 9
- 4 7 8
- */
- 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