For more information about this question, see the number of snake-type fields in the algorithm competition getting started classic by Liu rujia.
AC code:
1 # include <stdio. h> 2 # include <string. h> 3 # define Max 30 4 5 Int main () 6 {7 int n, x, y, TOT = 0; 8 int arry [Max] [Max]; 9 While (scanf ("% d", & N )! = EOF) 10 {11 memset (Arry, 0, sizeof (arry); 12 tot = arry [x = 0] [Y = 0] = 1; 13 14 While (Y + 1 <n &&! Arry [x] [Y + 1]) arry [x] [++ y] = ++ tot; 15 while (x + 1 <n &&! Arry [x + 1] [Y]) arry [++ x] [Y] = ++ tot; 16 while (Y-1> = 0 &&! Arry [x] [Y-1]) arry [x] [-- y] = ++ tot; 17 while (x-1> = 0 &&! Arry [x-1] [Y]) arry [-- X] [Y] = ++ tot; 18 19 int I, j; 20 for (I = 0; I <N; ++ I) 21 {22 for (j = 0; j <n; ++ J) 23 if (arry [I] [J]) 24 printf ("% 3d ", arry [I] [J]); 25 else printf (""); 26 printf ("\ n"); 27} 28} 29 30 return 0; 31}