Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
4-queens via recursive backtracking + List.fold_left. Returns 2 (the two solutions of 4-queens). Per-program timeout in run.sh bumped to 240s — the tree-walking interpreter is slow on heavy recursion but correct. The substrate handles full backtracking + safe-check recursion + list-driven candidate enumeration end-to-end.
36 lines
720 B
OCaml
36 lines
720 B
OCaml
(* Baseline: n-queens count for n=6.
|
||
We count placements of n queens on an n×n board such that no two
|
||
share a row, column, or diagonal. *)
|
||
|
||
let safe q queens =
|
||
let rec go qs offset =
|
||
match qs with
|
||
| [] -> true
|
||
| h :: t ->
|
||
if h = q then false
|
||
else if h - q = offset then false
|
||
else if q - h = offset then false
|
||
else go t (offset + 1)
|
||
in
|
||
go queens 1
|
||
;;
|
||
|
||
let rec range a b =
|
||
if a > b then [] else a :: range (a + 1) b
|
||
;;
|
||
|
||
let rec solve n queens row =
|
||
if row > n then 1
|
||
else
|
||
List.fold_left
|
||
(fun acc col ->
|
||
if safe col queens then
|
||
acc + solve n (col :: queens) (row + 1)
|
||
else
|
||
acc)
|
||
0
|
||
(range 1 n)
|
||
;;
|
||
|
||
solve 4 [] 1
|