fork download
  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <malloc.h>
  4.  
  5. void buildSnailReverse(int* a, int w, int h)
  6. {
  7. int s=w*h,n=0,d=0,j,i=s-w,r=3;
  8. int v[4]={1,w,-1,-w};
  9. (w>h?h:w+1)%2?i=w-1,r=1:0; //a
  10. w-h?0:w%2?i=s-1,r=3:0; //b
  11. while(n<s) {
  12. a[i]=s-n++;
  13. j=i+v[d];
  14. if(j<0||j>=s||(!d%2&&i/w!=j/w)||a[j]) d=d+r&3;
  15. i+=v[d];
  16. }
  17. }
  18.  
  19. void printArray(int* a, int w, int h)
  20. {
  21. int i, j;
  22. for (i = 0; i < h; ++i) {
  23. for (j = 0; j < w; ++j) {
  24. printf("%2d ", a[i * w + j]);
  25. }
  26. printf("\n");
  27. }
  28. printf("\n");
  29. }
  30.  
  31. void makeAndPrintSnail(int w, int h)
  32. {
  33. int *a;
  34. a = (int*)calloc(w * h, sizeof(int));
  35. buildSnailReverse(a, w, h);
  36. printArray(a, w, h);
  37. free(a);
  38. }
  39.  
  40. int main()
  41. {
  42. makeAndPrintSnail(3,3);
  43. makeAndPrintSnail(4,4);
  44. makeAndPrintSnail(5,5);
  45. makeAndPrintSnail(6,6);
  46.  
  47. makeAndPrintSnail(3,10);
  48. makeAndPrintSnail(4,10);
  49. makeAndPrintSnail(5,10);
  50. makeAndPrintSnail(5,11);
  51. makeAndPrintSnail(4,11);
  52. makeAndPrintSnail(3,11);
  53.  
  54. makeAndPrintSnail(10,3);
  55. makeAndPrintSnail(10,4);
  56. makeAndPrintSnail(10,5);
  57. makeAndPrintSnail(11,5);
  58. makeAndPrintSnail(11,4);
  59. makeAndPrintSnail(11,3);
  60. return 0;
  61. }
Success #stdin #stdout 0s 1920KB
stdin
Standard input is empty
stdout
 5  6  7 
 4  1  8 
 3  2  9 

 7  6  5 16 
 8  1  4 15 
 9  2  3 14 
10 11 12 13 

17 18 19 20 21 
16  5  6  7 22 
15  4  1  8 23 
14  3  2  9 24 
13 12 11 10 25 

21 20 19 18 17 36 
22  7  6  5 16 35 
23  8  1  4 15 34 
24  9  2  3 14 33 
25 10 11 12 13 32 
26 27 28 29 30 31 

17 18 19 
16  1 20 
15  2 21 
14  3 22 
13  4 23 
12  5 24 
11  6 25 
10  7 26 
 9  8 27 
30 29 28 

19 18 17 40 
20  1 16 39 
21  2 15 38 
22  3 14 37 
23  4 13 36 
24  5 12 35 
25  6 11 34 
26  7 10 33 
27  8  9 32 
28 29 30 31 

33 34 35 36 37 
32 13 14 15 38 
31 12  1 16 39 
30 11  2 17 40 
29 10  3 18 41 
28  9  4 19 42 
27  8  5 20 43 
26  7  6 21 44 
25 24 23 22 45 
50 49 48 47 46 

37 38 39 40 41 
36 15 16 17 42 
35 14  1 18 43 
34 13  2 19 44 
33 12  3 20 45 
32 11  4 21 46 
31 10  5 22 47 
30  9  6 23 48 
29  8  7 24 49 
28 27 26 25 50 
55 54 53 52 51 

21 20 19 44 
22  1 18 43 
23  2 17 42 
24  3 16 41 
25  4 15 40 
26  5 14 39 
27  6 13 38 
28  7 12 37 
29  8 11 36 
30  9 10 35 
31 32 33 34 

19 20 21 
18  1 22 
17  2 23 
16  3 24 
15  4 25 
14  5 26 
13  6 27 
12  7 28 
11  8 29 
10  9 30 
33 32 31 

17 16 15 14 13 12 11 10  9 30 
18  1  2  3  4  5  6  7  8 29 
19 20 21 22 23 24 25 26 27 28 

19 20 21 22 23 24 25 26 27 28 
18  1  2  3  4  5  6  7  8 29 
17 16 15 14 13 12 11 10  9 30 
40 39 38 37 36 35 34 33 32 31 

33 32 31 30 29 28 27 26 25 50 
34 13 12 11 10  9  8  7 24 49 
35 14  1  2  3  4  5  6 23 48 
36 15 16 17 18 19 20 21 22 47 
37 38 39 40 41 42 43 44 45 46 

37 36 35 34 33 32 31 30 29 28 55 
38 15 14 13 12 11 10  9  8 27 54 
39 16  1  2  3  4  5  6  7 26 53 
40 17 18 19 20 21 22 23 24 25 52 
41 42 43 44 45 46 47 48 49 50 51 

21 22 23 24 25 26 27 28 29 30 31 
20  1  2  3  4  5  6  7  8  9 32 
19 18 17 16 15 14 13 12 11 10 33 
44 43 42 41 40 39 38 37 36 35 34 

19 18 17 16 15 14 13 12 11 10 33 
20  1  2  3  4  5  6  7  8  9 32 
21 22 23 24 25 26 27 28 29 30 31