Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
59
Task/N-queens-problem/OCaml/n-queens-problem-1.ocaml
Normal file
59
Task/N-queens-problem/OCaml/n-queens-problem-1.ocaml
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
(* Authors: Nicolas Barnier, Pascal Brisset
|
||||
Copyright 2004 CENA. All rights reserved.
|
||||
This code is distributed under the terms of the GNU LGPL *)
|
||||
|
||||
open Facile
|
||||
open Easy
|
||||
|
||||
(* Print a solution *)
|
||||
let print queens =
|
||||
let n = Array.length queens in
|
||||
if n <= 10 then (* Pretty printing *)
|
||||
for i = 0 to n - 1 do
|
||||
let c = Fd.int_value queens.(i) in (* queens.(i) is bound *)
|
||||
for j = 0 to n - 1 do
|
||||
Printf.printf "%c " (if j = c then '*' else '-')
|
||||
done;
|
||||
print_newline ()
|
||||
done
|
||||
else (* Short print *)
|
||||
for i = 0 to n-1 do
|
||||
Printf.printf "line %d : col %a\n" i Fd.fprint queens.(i)
|
||||
done;
|
||||
flush stdout;
|
||||
;;
|
||||
|
||||
(* Solve the n-queens problem *)
|
||||
let queens n =
|
||||
(* n decision variables in 0..n-1 *)
|
||||
let queens = Fd.array n 0 (n-1) in
|
||||
|
||||
(* 2n auxiliary variables for diagonals *)
|
||||
let shift op = Array.mapi (fun i qi -> Arith.e2fd (op (fd2e qi) (i2e i))) queens in
|
||||
let diag1 = shift (+~) and diag2 = shift (-~) in
|
||||
|
||||
(* Global constraints *)
|
||||
Cstr.post (Alldiff.cstr queens);
|
||||
Cstr.post (Alldiff.cstr diag1);
|
||||
Cstr.post (Alldiff.cstr diag2);
|
||||
|
||||
(* Heuristic Min Size, Min Value *)
|
||||
let h a = (Var.Attr.size a, Var.Attr.min a) in
|
||||
let min_min = Goals.Array.choose_index (fun a1 a2 -> h a1 < h a2) in
|
||||
|
||||
(* Search goal *)
|
||||
let labeling = Goals.Array.forall ~select:min_min Goals.indomain in
|
||||
|
||||
(* Solve *)
|
||||
let bt = ref 0 in
|
||||
if Goals.solve ~control:(fun b -> bt := b) (labeling queens) then begin
|
||||
Printf.printf "%d backtracks\n" !bt;
|
||||
print queens
|
||||
end else
|
||||
prerr_endline "No solution"
|
||||
|
||||
let _ =
|
||||
if Array.length Sys.argv <> 2
|
||||
then raise (Failure "Usage: queens <nb of queens>");
|
||||
Gc.set ({(Gc.get ()) with Gc.space_overhead = 500}); (* May help except with an underRAMed system *)
|
||||
queens (int_of_string Sys.argv.(1));;
|
||||
32
Task/N-queens-problem/OCaml/n-queens-problem-2.ocaml
Normal file
32
Task/N-queens-problem/OCaml/n-queens-problem-2.ocaml
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
let solutions n =
|
||||
|
||||
let show board =
|
||||
let pr v =
|
||||
for i = 1 to n do
|
||||
print_string (if i=v then " q" else " _");
|
||||
done;
|
||||
print_newline() in
|
||||
List.iter pr board;
|
||||
print_newline() in
|
||||
|
||||
let rec safe i j k = function
|
||||
| [] -> true
|
||||
| h::t -> h<>i && h<>j && h<>k && safe i (j+1) (k-1) t in
|
||||
|
||||
let rec loop col p =
|
||||
for i = 1 to n
|
||||
do
|
||||
if safe i (i+1) (i-1) p then
|
||||
let p' = i::p in
|
||||
if col = n then show p'
|
||||
else loop (col+1) p'
|
||||
done in
|
||||
|
||||
loop 1 [] in
|
||||
|
||||
let n =
|
||||
if Array.length Sys.argv > 1
|
||||
then int_of_string Sys.argv.(1)
|
||||
else 8 in
|
||||
|
||||
solutions n
|
||||
Loading…
Add table
Add a link
Reference in a new issue