scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

0041-queen.c (1148B)


      1 int *calloc(int, int);
      2 
      3 int N;
      4 int *t;
      5 
      6 int
      7 chk(int x, int y)
      8 {
      9         int i;
     10         int r;
     11 
     12         for (r=i=0; i<8; i++) {
     13                 r = r + t[x + 8*i];
     14                 r = r + t[i + 8*y];
     15                 if (x+i < 8 & y+i < 8)
     16                         r = r + t[x+i + 8*(y+i)];
     17                 if (x+i < 8 & y-i >= 0)
     18                         r = r + t[x+i + 8*(y-i)];
     19                 if (x-i >= 0 & y+i < 8)
     20                         r = r + t[x-i + 8*(y+i)];
     21                 if (x-i >= 0 & y-i >= 0)
     22                         r = r + t[x-i + 8*(y-i)];
     23         }
     24         return r;
     25 }
     26 
     27 int
     28 go(int n, int x, int y)
     29 {
     30         if (n == 8) {
     31                 N++;
     32                 return 0;
     33         }
     34         for (; y<8; y++) {
     35                 for (; x<8; x++)
     36                         if (chk(x, y) == 0) {
     37                                 t[x + 8*y]++;
     38                                 go(n+1, x, y);
     39                                 t[x + 8*y]--;
     40                         }
     41                 x = 0;
     42         }
     43 	return 0;
     44 }
     45 
     46 int
     47 main()
     48 {
     49         t = calloc(64, sizeof(int));
     50         go(0, 0, 0);
     51         if(N != 92)
     52         	return 1;
     53         return 0;
     54 }
     55