fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. int* buildSnailReverse( int* a, int w, int h )
  8. {
  9. int
  10. u[] = { 1, w,-1,-w},
  11. d = w < h,
  12. z = ((d? w: h) - 1 >> 1) * (w + 1) + (~w & d),
  13. n = d? h - w: w - h,
  14. p = 0,
  15. c = n,
  16. i = 0;
  17. while(i < w * h)
  18. {
  19. a[z] = ++i;
  20. z += u[d];
  21. if (!c--)
  22. {
  23. ++d &= 3, c = n ^= p ^= n ^= p;
  24. p++;
  25. }
  26. }
  27. return a;
  28. }
  29.  
  30. void printArray(int* a, int w, int h)
  31. {
  32. for(int y = 0; y < h; y++)
  33. {
  34. for(int x = 0; x < w; x++)
  35. printf("%3d", a[x + y * w]);
  36. cout << endl;
  37. }
  38. }
  39.  
  40. int main()
  41. {
  42. int w, h;
  43. cin >> w >> h;
  44. int* a = new int[w * h];
  45. buildSnailReverse(a, w, h);
  46. printArray(a, w, h);
  47. delete[] a;
  48. fflush(stdin);
  49. getchar();
  50. return 0;
  51. }
Success #stdin #stdout 0s 15240KB
stdin
8
5
stdout
 33 34 35 36 37 38 39 40
 32 13 14 15 16 17 18 19
 31 12  1  2  3  4  5 20
 30 11 10  9  8  7  6 21
 29 28 27 26 25 24 23 22