aboutsummaryrefslogtreecommitdiff
path: root/nqueens.c
diff options
context:
space:
mode:
authorAgney Lopes Roth Ferraz <agney@debian.org>2009-03-28 22:55:02 -0300
committerSimon Quigley <tsimonq2@ubuntu.com>2017-06-19 14:38:48 -0500
commit1b6665085f47e0a86e4bebd5e313a2ab63600eb4 (patch)
tree6b7dedc1886b42d07cc48359470fe3f595500cc8 /nqueens.c
parent819c0c3382b06fc0f0a1679465966f811aa2e0f8 (diff)
parent4979bb6cbbbe39eb44c32530cd13f86bf44e5d77 (diff)
Import Debian changes 0.5c-1
hardinfo (0.5c-1) unstable; urgency=low * New upstream version. (Closes: #517591, #511237, #457703, #519256, #449250, #457820, #497758)
Diffstat (limited to 'nqueens.c')
-rw-r--r--nqueens.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/nqueens.c b/nqueens.c
new file mode 100644
index 00000000..838731c4
--- /dev/null
+++ b/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;
+}