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 int myabs(int i) {
35 boolean place_ok (int i) {
36 // return whether queen in column i is
37 // not in check from queens left of it
41 printf("POK: %d\n", i);
44 if ((row[j] == row[i]) || ((myabs(row[i]-row[j])) == (i-j))) {
57 // return the number of solutions to the n-queens problem
61 row = (void *)malloc(sizeof(*row) * n);
65 while ((row[c] < n) && (!place_ok(c))) {
68 printf("RC: %d\n", row[c]);
69 if (row[c] < n) { // successfully placed at (c,row[c])
77 else // dead end, track back
85 static void usage (const char *progname) {
86 printf("usage: %s\n", progname);
90 int main (int argc, char *argv[]) {
104 printf("The %d-queens problem has %d solutions.\n", n, solve(n));