fork download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4.  
  5. void s(int**m,int*v,int n,int h){int r=h,c=h,i=0,j,l=n-2*h-1;if(l>-1){do{*v++=m[r][c];l?(j=i/l)<1?c++:j<2?r++:j<3?c--:r--:l;}while(++i<4*l);s(m,v,n,h+1);}}
  6.  
  7. using V=std::vector<int>;
  8. std::vector<V> x{{1, 2, 3, 4}, {12, 13, 14, 5}, {11, 16, 15, 6}, {10, 9, 8, 7}};
  9. std::vector<V> y{{1, 2, 3}, {8, 9, 4}, {7, 6, 5}};
  10. std::vector<V> z;
  11.  
  12. int** make_array(const std::vector<V>& v)
  13. {
  14. int **m = new int*[v.size()];
  15. for(int i = 0; i < v.size(); ++i)
  16. m[i] = new int[v.size()];
  17. for(int i = 0; i < v.size(); ++i)
  18. for(int j = 0; j < v.size(); ++j)
  19. m[i][j] = v[i][j];
  20. return m;
  21. }
  22.  
  23. int main()
  24. {
  25. V v(x.size()*x.size()), w(y.size()*y.size()), t(z.size()*z.size());
  26. s(make_array(x), v.data(), x.size(), 0);
  27. for (auto i : v) std::cout << i << " ";
  28. std::cout << "\n";
  29. s(make_array(y), w.data(), y.size(), 0);
  30. for (auto i : w) std::cout << i << " ";
  31. std::cout << "\n";
  32. s(make_array(z), t.data(), z.size(), 0);
  33. for (auto i : t) std::cout << i << " ";
  34. std::cout << "\n";
  35. }
  36.  
  37.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
1 2 3 4 5 6 7 8 9