This program is intended to solve n-queens problem through bitwise approach using go programming language. This algorithm used on these codes is based on a paper by Martin Richards titled Backtracking Algorithms in MCPL using Bit Patterns and Recursion.
No need to install, just download the pre-compiled binary on our releases tab.
In order to get these source codes, you need to have at least go1.9 installed on your system and then you can easily get it with
go get -u github.com/rimaulana/nqueens
To be able to compile the source code using Makefile we provided, you will at least need shell (git shell or linux shell) that can run make. you can simply run
make release
that command will create a folder release and inside there will be three binary for windows, linux and darwin operating system and all will be for amd64 processor architecture. However, if you prefer to custom compile these codes, you can use go compiler.
go build
There are several flag or params you can supply when executing the program. they are:
- size (int)(n), this flag will tell the program to find the solution(s) of n x n board with n number of queens.
- sample (int)(m), this flag will tell the program to print m sample of board, if you just interested on the number of solution just don't add this flag when executing the program to deactivate this option.
Sample of command to find solution of 4 Queens on 4x4 boards with 2 samples is.
./nqueens-v1.0.0-linux-amd64 --size=4 --sample=2
That command will give us output like the following
┌─┬─┬─┬─┐
│ │Q│ │ │
├─┼─┼─┼─┤
│ │ │ │Q│
├─┼─┼─┼─┤
│Q│ │ │ │
├─┼─┼─┼─┤
│ │ │Q│ │
└─┴─┴─┴─┘
┌─┬─┬─┬─┐
│ │ │Q│ │
├─┼─┼─┼─┤
│Q│ │ │ │
├─┼─┼─┼─┤
│ │ │ │Q│
├─┼─┼─┼─┤
│ │Q│ │ │
└─┴─┴─┴─┘
2 boards are drawn in 201.767µs
There are 2 solutions for board size 4
Solutions are generated in 7.922µs
Martin Richards, Backtracking Algorithms in MCPL using Bit Patterns and Recursion, Computer Laboratory, University of Cambridge, February 23, 2009, http://www.cl.cam.ac.uk/users/mr/