Test every book test case
[tiger.ml.git] / compiler / testcases / queens.tig
diff --git a/compiler/testcases/queens.tig b/compiler/testcases/queens.tig
new file mode 100644 (file)
index 0000000..1254b12
--- /dev/null
@@ -0,0 +1,42 @@
+/* A program to solve the 8-queens problem */
+
+let
+  var N := 8
+
+  type intArray = array of int
+
+  var row := intArray [ N ] of 0
+  var col := intArray [ N ] of 0
+  var diag1 := intArray [N+N-1] of 0
+  var diag2 := intArray [N+N-1] of 0
+
+  function printboard() = (
+    for i := 0 to N-1 do (
+      for j := 0 to N-1 do print(if col[i]=j then " O" else " .");
+      print("\n")
+    );
+    print("\n")
+  )
+
+  function try(c:int) = (
+    /*  for i:= 0 to c do print("."); print("\n"); flush();*/
+    if c=N
+    then printboard()
+    else
+      for r := 0 to N-1
+      do
+        if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0
+        then (
+          row[r]       := 1;
+          diag1[r+c]   := 1;
+          diag2[r+7-c] := 1;
+          col[c]       := r;
+          try(c+1);
+          row[r]       := 0;
+          diag1[r+c]   := 0;
+          diag2[r+7-c] := 0
+        )
+  )
+in
+  try(0)
+end
This page took 0.031944 seconds and 4 git commands to generate.