3 * File name: test/Queens.c
4 * Purpose: solve the queens problem
5 * Author: Markus Armbruster (in sather-k)
6 * Modified by: Michael Beck (for GCC-firm)
9 * Copyright: (c) 2001 Universitaet Karlsruhe
13 -- The notorious n-queens problem (C.F. Gauss, 1850)
14 -- Copyright (C) 1996 Markus Armbruster
27 // queen in column c is at row[c]
29 static inline int myabs(int i) {
35 static inline boolean place_ok (int i, const int *r, int ri) {
36 // return whether queen in column i is
37 // not in check from queens left of it
43 if ((rj == ri) || ((myabs(ri-rj)) == (i-j))) {
55 // return the number of solutions to the n-queens problem
60 row = malloc(sizeof(*row) * n);
67 } while ((rc < n) && (!place_ok(c, row, rc)));
69 if (rc < n) { // successfully placed at (c,row[c])
89 static void usage (const char *progname) {
90 printf("usage: %s\n", progname);
94 int main (int argc, char *argv[]) {
108 printf("The %d-queens problem has %d solutions.\n", n, solve(n));