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 »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: