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

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

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

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

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

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 

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

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 

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

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 

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

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

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 

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