N Queens in k

I read the Eight Queens article on nsl.com. It describes a nice algorithm to solve the problem. The given code however does not work in the current q/k4 interpreter. (The dvl function is not implemented.)

So I wrote my own:

q7:{[n]n(,/{y,/:&~x in,/y+|-1 0 1*/:1+!#y}[!n]')/,()}

It returns all solutions for a board of size n x n. For n larger than 8, it could probably be made more efficient by doing some pre-computation. The code (including several other versions) is here: qns_k.

Advertisements
Posted in k&q. Tags: , . Leave a Comment »