aboutsummaryrefslogtreecommitdiff
path: root/hardinfo2/nqueens.c
diff options
context:
space:
mode:
authorLeandro Augusto Fogolin Pereira <leandro@zorg.(none)>2009-01-02 18:13:39 -0200
committerLeandro Augusto Fogolin Pereira <leandro@zorg.(none)>2009-01-02 18:13:39 -0200
commit1ed9b3b12f9de4bf06b23297e68ec85d83e76cc5 (patch)
tree043e2ebb7267677b5ef6119c26e66dd2039ccfb2 /hardinfo2/nqueens.c
parenta62ba151352705ce589855e6596b5a580cfa198f (diff)
Remove ZLib benchmark; add N-Queens benchmark
Diffstat (limited to 'hardinfo2/nqueens.c')
-rw-r--r--hardinfo2/nqueens.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/hardinfo2/nqueens.c b/hardinfo2/nqueens.c
new file mode 100644
index 00000000..9d36a82e
--- /dev/null
+++ b/hardinfo2/nqueens.c
@@ -0,0 +1,35 @@
+/*
+ * 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;
+}