summaryrefslogtreecommitdiff
path: root/modules/benchmark/nqueens.c
diff options
context:
space:
mode:
authorLeandro A. F. Pereira <leandro@hardinfo.org>2010-05-03 09:27:26 -0300
committerLeandro A. F. Pereira <leandro@hardinfo.org>2010-05-03 21:08:06 -0300
commit9273c075a2f993c5154614b70233d8f74515c851 (patch)
treeeb72a8c58e6bc8f4ca3b739d28fbecc269c0052d /modules/benchmark/nqueens.c
parent9a50155ec3e27aa6cedf3f118196f1947c769a29 (diff)
Move files from hardinfo2 to root.
Diffstat (limited to 'modules/benchmark/nqueens.c')
-rw-r--r--modules/benchmark/nqueens.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/modules/benchmark/nqueens.c b/modules/benchmark/nqueens.c
new file mode 100644
index 00000000..838731c4
--- /dev/null
+++ b/modules/benchmark/nqueens.c
@@ -0,0 +1,37 @@
+/*
+ * N-Queens Problem Solver
+ * Found somewhere on the Internet; can't remember where. Possibly Wikipedia.
+ */
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+
+#define QUEENS 11
+
+int row[QUEENS];
+
+bool safe(int x, int y)
+{
+ int i;
+ for (i = 1; i <= y; i++)
+ if (row[y - i] == x || row[y - i] == x - i || row[y - i] == x + i)
+ return false;
+ return true;
+}
+
+int nqueens(int y)
+{
+ int x;
+
+ for (x = 0; x < QUEENS; x++) {
+ if (safe((row[y - 1] = x), y - 1)) {
+ if (y < QUEENS) {
+ nqueens(y + 1);
+ } else {
+ break;
+ }
+ }
+ }
+
+ return 0;
+}