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
29 // queen in column c is at row[c]
31 static inline int myabs(int i) {
37 static inline boolean place_ok (int i, const int *r, int ri) {
38 // return whether queen in column i is
39 // not in check from queens left of it
49 if ((rj == ri) || ((myabs(ri-rj)) == (i-j))) {
61 if ((rj == ri) || ((myabs(ri-rj)) == (i-j))) {
74 // return the number of solutions to the n-queens problem
79 row = malloc(sizeof(*row) * n);
86 } while ((rc < n) && (!place_ok(c, row, rc)));
88 if (rc < n) { // successfully placed at (c,row[c])
108 static void usage (const char *progname) {
109 printf("usage: %s\n", progname);
113 int main (int argc, char *argv[]) {
127 printf("The %d-queens problem has %d solutions.\n", n, solve(n));