]> git.sur5r.net Git - cc65/blob - test/ref/8q.c
remote TABs in doc/ and test/
[cc65] / test / ref / 8q.c
1 /*
2   !!DESCRIPTION!! solves the "8 queens" chess problem
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 #include <stdio.h>
8
9 int up[15], down[15], rows[8], x[8];
10 void queens(int c);
11 void print(void);
12
13 int main(void)
14 {
15         int i;
16         for (i = 0; i < 15; i++)
17                 up[i] = down[i] = 1;
18         for (i = 0; i < 8; i++)
19                 rows[i] = 1;
20         queens(0);
21         return 0;
22 }
23
24 void queens(int c)
25 {
26         int r;
27
28         for (r = 0; r < 8; r++)
29                 if (rows[r] && up[r-c+7] && down[r+c]) {
30                         rows[r] = up[r-c+7] = down[r+c] = 0;
31                         x[c] = r;
32                         if (c == 7)
33                                 print();
34                         else
35                                 queens(c + 1);
36                         rows[r] = up[r-c+7] = down[r+c] = 1;
37                 }
38 }
39
40 void print(void)
41 {
42         int k;
43
44         for (k = 0; k < 8; k++) {
45                 printf("%c", x[k]+'1');
46                 if(k<7) printf(" ");
47         }
48         printf("\n");
49 }