From 6f3aca9d502b38cd65c0bc4e1869b35ee360e267 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 7 Mar 2012 12:18:19 +0100 Subject: [PATCH] Added example/queens.jl --- examples/queens.jl | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/queens.jl diff --git a/examples/queens.jl b/examples/queens.jl new file mode 100644 index 0000000000000..184fcc13ffc6d --- /dev/null +++ b/examples/queens.jl @@ -0,0 +1,29 @@ +addqueen(queens::Array{Vector{Int}}, queen::Vector{Int}) = push(copy(queens), queen) + +hitsany(queen::Vector{Int}, queens::Array{Vector{Int}}) = any(map((x) -> hits(queen, x), queens)) +hits(a::Array{Int}, b::Array{Int}) = any(a == b) || abs(a-b)[1] == abs(a-b)[2] + +function solve(x::Int, y::Int, n::Int, d::Array{Vector{Int}}) + if n == 0 + return d + end + for px = 1:x + for py = 1:y + if !hitsany([px, py], d) + s = solve(x, y, n-1, addqueen(d, [px, py])) + if s != None + return s + end + end + end + end + None +end + +solve(x, y, n) = solve(x, y, n, Array(Vector{Int}, 0)) + +for i = 1:8 + print("Solve for $i\n") + print(solve(8, 8, i)) + print("\n") +end \ No newline at end of file