Advertisement
Glenpl

Untitled

Jun 29th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  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. /*
  71. 00 01 02
  72. 10 11 12
  73. 20 21 22
  74.  
  75. 1 2 3
  76. 4 5 6
  77. 7 8 9
  78.  
  79. 2 3 6
  80. 1 5 9
  81. 4 7 8
  82.  
  83. 0 1 2
  84. 1 1 2
  85. 2 2 2
  86.  
  87. 00 01 02
  88. 10 11 12
  89. 20 21 22
  90.  
  91. 0,0 > 0,1 1
  92. 0,1 > 0,2 2
  93. 0,2 > 1,2 3
  94. 1,2 > 2,2 4
  95. 2,2 > 2,1 5
  96. 2,1 > 2,0 6
  97. 2,0 > 1,0 7
  98.  
  99. for(int i = 0; i < x.size(); i++)
  100.     for(int j = 0; j < x.size(); j++)
  101.  
  102. 0 1 2 3
  103. 1 1 2 3
  104. 2 2 2 3
  105. 3 3 3 3
  106.  
  107. 0,0 > 0,1 1
  108. 0,1 > 0,2 2
  109. 0,2 > 0,3 3
  110. 0,3 > 1,3 4
  111. 1,3 > 2,3 5
  112. 2,3 > 3,3 6
  113. 3,3 > 3,2 7
  114. 3,2 > 3,1 8
  115. 3,1 > 3,0 9
  116. 3,0 > 2,0 10
  117. 2,0 > 1,0 11
  118.  
  119. 0 1 2 3 4
  120. 1 1 2 3 4
  121. 2 2 2 3 4
  122. 3 3 3 3 4
  123. 4 4 4 4 4
  124.  
  125. kwadrat 5*5 = 4*4-1 kroków
  126.  
  127. 0 1 2 3 4 5
  128. 1 1 2 3 4 5
  129. 2 2 2 3 4 5
  130. 3 3 3 3 4 5
  131. 4 4 4 4 4 5
  132.  
  133. 17
  134. 5 * 6 - 17
  135.  
  136. 6 + 12 - 1 = 18 - 1 = 17
  137.  
  138. (bok - 2)
  139. |
  140. |
  141. |
  142. * 2
  143. + bok
  144. - - -
  145. * 2 - 1
  146.  
  147. 2 * (wierszy - 2) + 2 * kolumny - 1
  148.  
  149. 2 3 6
  150. 1 5 9
  151. 4 7 8
  152. */
  153.  
  154. std::vector<std::vector<int>> createVector(int rows, int columns)
  155. {
  156.     std::vector<std::vector<int>> v;
  157.     for(int i = 0; i < rows; i++)
  158.         v.push_back( std::vector<int>(columns) );
  159.     return v;
  160. }
  161.  
  162. void writeArray(std::vector<std::vector<int>> arr)
  163. {
  164.     for(auto x : arr)
  165.     {
  166.         for(auto y : x)
  167.             std::cout << y << " ";
  168.         std::cout << "\n";
  169.     }
  170. }
  171.  
  172. void rotateArray(std::vector<std::vector<int>> &arr, int rows, int columns)
  173. {
  174.     int steps = 2 * (rows-1) + (2 * (columns-1)) - 1;
  175.     Index a(0, 0, arr.size(), arr[0].size()), b(0, 1, arr.size(), arr[0].size());
  176.     for(int i = 0; i < steps; i++)
  177.     {
  178.         std::swap( arr[a.getX()][a.getY()], arr[b.getX()][b.getY()]  );
  179.         a.moveIndex();
  180.         b.moveIndex();
  181.     }
  182. }
  183.  
  184. int main()
  185. {
  186.     int t, l, k;
  187.     std::cin>>t;
  188.     for(int i = 0; i < t; i++)
  189.     {
  190.         std::cin>>l>>k;
  191.         auto arr = createVector(l, k);
  192.         for(int j = 0; j < l; j++)
  193.             for(int h = 0; h < k; h++)
  194.                 std::cin>>arr[j][h];
  195.  
  196.         rotateArray(arr, l, k);
  197.         writeArray(arr);
  198.     }
  199.  
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement