2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
77
Task/N-queens-problem/Julia/n-queens-problem-1.julia
Normal file
77
Task/N-queens-problem/Julia/n-queens-problem-1.julia
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#!/usr/bin/env julia
|
||||
|
||||
__precompile__(true)
|
||||
|
||||
"""
|
||||
# EightQueensPuzzle
|
||||
|
||||
Ported to **Julia** from examples in several languages from
|
||||
here: https://hbfs.wordpress.com/2009/11/10/is-python-slow
|
||||
"""
|
||||
module EightQueensPuzzle
|
||||
|
||||
export main
|
||||
|
||||
type Board
|
||||
cols::Int
|
||||
nodes::Int
|
||||
diag45::Int
|
||||
diag135::Int
|
||||
solutions::Int
|
||||
|
||||
Board() = new(0, 0, 0, 0, 0)
|
||||
end
|
||||
|
||||
"Marks occupancy."
|
||||
function mark!(b::Board, k::Int, j::Int)
|
||||
b.cols $= (1 << j)
|
||||
b.diag135 $= (1 << (j+k))
|
||||
b.diag45 $= (1 << (32+j-k))
|
||||
end
|
||||
|
||||
"Tests if a square is menaced."
|
||||
function test(b::Board, k::Int, j::Int)
|
||||
b.cols & (1 << j) +
|
||||
b.diag135 & (1 << (j+k)) +
|
||||
b.diag45 & (1 << (32+j-k)) == 0
|
||||
end
|
||||
|
||||
"Backtracking solver."
|
||||
function solve!(b::Board, niv::Int, dx::Int)
|
||||
if niv > 0
|
||||
for i in 0:dx-1
|
||||
if test(b, niv, i) == true
|
||||
mark!(b, niv, i)
|
||||
solve!(b, niv-1, dx)
|
||||
mark!(b, niv, i)
|
||||
end
|
||||
end
|
||||
else
|
||||
for i in 0:dx-1
|
||||
if test(b, 0, i) == true
|
||||
b.solutions += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
b.nodes += 1
|
||||
b.solutions
|
||||
end
|
||||
|
||||
"C/C++-style `main` function."
|
||||
function main()
|
||||
for n = 1:17
|
||||
gc()
|
||||
b = Board()
|
||||
@show n
|
||||
print("elapsed:")
|
||||
solutions = @time solve!(b, n-1, n)
|
||||
@show solutions
|
||||
println()
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
using EightQueensPuzzle
|
||||
|
||||
main()
|
||||
68
Task/N-queens-problem/Julia/n-queens-problem-2.julia
Normal file
68
Task/N-queens-problem/Julia/n-queens-problem-2.julia
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
juser@juliabox:~$ /opt/julia-0.5/bin/julia eight_queen_puzzle.jl
|
||||
n = 1
|
||||
elapsed: 0.000001 seconds
|
||||
solutions = 1
|
||||
|
||||
n = 2
|
||||
elapsed: 0.000001 seconds
|
||||
solutions = 0
|
||||
|
||||
n = 3
|
||||
elapsed: 0.000001 seconds
|
||||
solutions = 0
|
||||
|
||||
n = 4
|
||||
elapsed: 0.000001 seconds
|
||||
solutions = 2
|
||||
|
||||
n = 5
|
||||
elapsed: 0.000003 seconds
|
||||
solutions = 10
|
||||
|
||||
n = 6
|
||||
elapsed: 0.000008 seconds
|
||||
solutions = 4
|
||||
|
||||
n = 7
|
||||
elapsed: 0.000028 seconds
|
||||
solutions = 40
|
||||
|
||||
n = 8
|
||||
elapsed: 0.000108 seconds
|
||||
solutions = 92
|
||||
|
||||
n = 9
|
||||
elapsed: 0.000463 seconds
|
||||
solutions = 352
|
||||
|
||||
n = 10
|
||||
elapsed: 0.002146 seconds
|
||||
solutions = 724
|
||||
|
||||
n = 11
|
||||
elapsed: 0.010646 seconds
|
||||
solutions = 2680
|
||||
|
||||
n = 12
|
||||
elapsed: 0.057603 seconds
|
||||
solutions = 14200
|
||||
|
||||
n = 13
|
||||
elapsed: 0.334600 seconds
|
||||
solutions = 73712
|
||||
|
||||
n = 14
|
||||
elapsed: 2.055078 seconds
|
||||
solutions = 365596
|
||||
|
||||
n = 15
|
||||
elapsed: 13.480449 seconds
|
||||
solutions = 2279184
|
||||
|
||||
n = 16
|
||||
elapsed: 97.192552 seconds
|
||||
solutions = 14772512
|
||||
|
||||
n = 17
|
||||
elapsed:720.314676 seconds
|
||||
solutions = 95815104
|
||||
Loading…
Add table
Add a link
Reference in a new issue