fork download
  1. #include <stdio.h>
  2.  
  3. int make_spiral(int *m, int s, int n, int hour, int is_cw);
  4.  
  5. int main(void)
  6. {
  7. int spiral_map[16][16];
  8. int y, x, n, h;
  9.  
  10. scanf("%d %d", &n, &h);
  11. make_spiral(spiral_map, 16, n, h, 1);
  12.  
  13. for (y = 0; y < n; y++){
  14. for (x = 0; x < n; x++){
  15. printf("%4d", spiral_map[y][x]);
  16. }
  17. puts("");
  18. }
  19.  
  20. return (0);
  21. }
  22.  
  23. int make_spiral(int *m, int s, int n, int hour, int is_cw)
  24. {
  25. int x, y;
  26. int c, l, v, i;
  27. int t[] = {0, -1, 0, 1, 0, -1, 0, 1};
  28. int *d = t + (hour / 3 % 4 + 4 & 3);
  29.  
  30. n & 1 || n++;
  31. x = y = n / 2;
  32. c = l = v = 0;
  33. n *= n;
  34.  
  35. for (m[y * s + x] = ++c; c < n; v &= 3){
  36. for (i = 0; i < l; i++){
  37. x += d[v + 1];
  38. y += d[v] * (is_cw ? 1 : -1);
  39. m[y * s + x] = ++c;
  40. }
  41. if (++v & 1) l++;
  42. }
  43. return (0);
  44. }
Success #stdin #stdout 0s 1836KB
stdin
5 9
stdout
  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