fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //#include <boost/multiprecision/cpp_int.hpp>
  4. //using namespace boost::multiprecision;
  5. #define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  6. #pragma GCC optimize "trapv"
  7. #pragma GCC optimize("Ofast")
  8. #define _GLIBCXX_DEBUG
  9. #define ll long long int
  10. #define ld long double
  11. #define ull unsigned long long int // ranges from (0 - twice of long long int)
  12. #define rep(i,a,n) for (ll i=a;i<n;i++)
  13. #define per(i,a,n) for (ll i=n-1;i>=a;i--)
  14. #define pb push_back
  15. #define mp make_pair
  16. #define vll vector<ll>
  17. #define mod 1000000007LL
  18. #define llpair pair<ll,ll>
  19. #define INF 1000000000000000000ll
  20. #define np next_permutation
  21. #define PI acos(-1)
  22. #define deb(x) cout<<#x<<" "<<x<<endl;
  23. #define rotate_left(vec,amt) rotate(vec.begin(),vec.begin()+amt,vec.end());
  24. #define rotate_right(vec,amt) rotate(vec.begin(),vec.begin()+vec.size()-amt,vec.end());
  25. #define all(x) x.begin(),x.end()
  26. #define sortall(x) sort(all(x))
  27. #define clr(x) memset(x,0,sizeof(x))
  28.  
  29. int a[305][305];
  30. vector<ll>matrix;
  31. vector<int>res;
  32. void spiralPrint(int m, int n, int a[305][305])
  33. {
  34. int i, k = 0, l = 0;
  35.  
  36. /* k - starting row index
  37.   m - ending row index
  38.   l - starting column index
  39.   n - ending column index
  40.   i - iterator
  41.   */
  42.  
  43. while (k < m && l < n) {
  44. /* Print the first row from
  45.   the remaining rows */
  46. for (i = l; i < n; ++i) {
  47. //cout << a[k][i] << " ";
  48. matrix.pb(a[k][i]);
  49. }
  50. k++;
  51.  
  52. /* Print the last column
  53.   from the remaining columns */
  54. for (i = k; i < m; ++i) {
  55. //cout << a[i][n - 1] << " ";
  56. matrix.pb(a[i][n-1]);
  57. }
  58. n--;
  59.  
  60. /* Print the last row from
  61.   the remaining rows */
  62. if (k < m) {
  63. for (i = n - 1; i >= l; --i) {
  64. //cout << a[m - 1][i] << " ";
  65. matrix.pb(a[m-1][i]);
  66. }
  67. m--;
  68. }
  69.  
  70. /* Print the first column from
  71.   the remaining columns */
  72. if (l < n) {
  73. for (i = m - 1; i >= k; --i) {
  74. //cout << a[i][l] << " ";
  75. matrix.pb(a[i][l]);
  76. }
  77. l++;
  78. }
  79.  
  80.  
  81. }
  82. }
  83.  
  84.  
  85. int z=0;
  86. void sendback(int m, int n, int a[305][305])
  87. {
  88. int i, k = 0, l = 0;
  89.  
  90. /* k - starting row index
  91.   m - ending row index
  92.   l - starting column index
  93.   n - ending column index
  94.   i - iterator
  95.   */
  96.  
  97. while (k < m && l < n) {
  98. /* Print the first row from
  99.   the remaining rows */
  100. for (i = l; i < n; ++i) {
  101. a[k][i] = res[z++];
  102. }
  103. k++;
  104.  
  105. /* Print the last column
  106.   from the remaining columns */
  107. for (i = k; i < m; ++i) {
  108. a[i][n - 1] = res[z++];
  109.  
  110. }
  111. n--;
  112.  
  113. /* Print the last row from
  114.   the remaining rows */
  115. if (k < m) {
  116. for (i = n - 1; i >= l; --i) {
  117. a[m - 1][i] =res[z++];
  118.  
  119. }
  120. m--;
  121. }
  122.  
  123. /* Print the first column from
  124.   the remaining columns */
  125. if (l < n) {
  126. for (i = m - 1; i >= k; --i) {
  127. a[i][l] = res[z++];
  128.  
  129. }
  130. l++;
  131. }
  132.  
  133.  
  134. }
  135. }
  136.  
  137. int main() {
  138. //auto start = chrono::high_resolution_clock::now();
  139. fio;
  140. ll t=1;
  141. //cin>>t;
  142. while(t--)
  143. {
  144. clr(a);
  145. ll m,n; cin>>m>>n;
  146. rep(i,0,m) rep(j,0,n) cin>>a[i][j];
  147.  
  148. //ll l; cin>>l;
  149. vector<int>vec; // it stores the no of elements for rotation
  150. char temp;
  151. int i=0;
  152. do {
  153. int x;
  154. scanf("%d%c", &x, &temp);
  155. vec.pb(x);
  156. i++;
  157. }while(temp != '\n');
  158.  
  159. matrix.clear();
  160. spiralPrint(m,n,a);
  161.  
  162.  
  163.  
  164. int tm=m,tn=n;
  165.  
  166.  
  167.  
  168.  
  169. ll b = 2*tm + 2*(tn-2);
  170. int lvl=1;
  171. vector<int>tt;
  172. res.clear();
  173. int k=0;
  174. while(matrix.size())
  175. {
  176. tt.clear();
  177. while(b--)
  178. {
  179. tt.pb(matrix[0]);
  180. matrix.erase(matrix.begin()+0);
  181. }
  182.  
  183.  
  184.  
  185. if(lvl%2)
  186. {
  187. if(b>=vec[k])
  188. {
  189. rotate_left(tt,b%vec[k]);
  190. }
  191. else
  192. {
  193.  
  194. rotate_left(tt,vec[k]);
  195. }
  196. k++;
  197. }
  198. else if(lvl%2==0)
  199. {
  200. if(b>=vec[k])
  201. {
  202. rotate_right(tt,b%vec[k]);
  203. }
  204. else
  205. {
  206.  
  207. rotate_right(tt,vec[k]);
  208. }
  209. k++;
  210. }
  211.  
  212.  
  213.  
  214.  
  215. for(int i=0;i<(int)tt.size();i++)
  216. res.pb(tt[i]);
  217. lvl++;
  218.  
  219. tm=tm-2; tn=tn-2;
  220. b=2*tm + 2*(tn-2);
  221. }
  222.  
  223.  
  224.  
  225.  
  226. sendback(m,n,a);
  227.  
  228.  
  229. for(int i=0;i<m;i++)
  230. {
  231. for(int j=0;j<n;j++)
  232. cout<<a[i][j]<<" ";
  233. cout<<"\n";
  234. }
  235.  
  236. }
  237. //auto finish = chrono::high_resolution_clock::now();
  238. //cerr << "Time elapsed: " << (chrono::duration<long double>(finish-start)).count() << "s\n";
  239. return 0;
  240.  
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
Time limit exceeded #stdin #stdout 5s 527972KB
stdin
4 4
1 2 3 4
2 3 4 5
2 4 5 6
2 3 4 5
2 2
stdout
Standard output is empty