Feb 12
2013

# A recursive algorithm for generating all N-Rooks solutions and a linear time N-Queens solution

Below is my interactive implementation of the famous N-Rooks and N-Queens problem. If you’re on a feed reader or have disabled for JavaScript some reason, you’re missing the main feature of this page! Click below on the board to add and remove pieces. The board indicates threatened pieces. Checking ‘All’ solutions for rooks shows a random solution and logs them all into the JS console.

Solve for:

All:
Click to show solutions

### The N-Rooks Problem

The N-Rooks problem is as follows: on a chessboard with N columns and N rows, how can you place N rooks such than none of them threaten each other? Finding a single solution is trivially simple– just place a rook at a corner and then place rooks along the diagonal, until reaching the opposite corner. Finding all solutions for a given N is a more difficult and more interesting problem. It also provided me with my first opportunity to surprise a few of my teachers at Hack Reactor!

### Generating the set of all N-Rooks solutions given the solutions to size N-1

This solution works as follows:

1. Take each board from the N-1 solution set
2. Append an empty square to each row. This creates an N by N-1 board.
3. Create a new row with one rook on the very last square
4. Create N new boards by inserting the new row into each possible place in the N by N-1 board.

Following this pattern, the final column will have exactly one rook and a board will be created for each possible configuration. Furthermore all possible solutions for each size N will be generated without examining a single board which is not a solution. It’s the rare case in which N! time is a good thing– there are exactly N! solutions for a board of a size N. Here’s the JavaScript:

### The N-Queens Problem

The N-Queens Problem is similar. Place N queens on an N row, N column board in such a way that none of them threaten each other. Since queens are such strong attackers, it’s a far, far more difficult than N rooks. The tactic most people take is to examine every possible board configuration, stopping once a row, column or diagonal conflict is encountered. I tried pruning the solution space by using my All N-Rooks solution as a starting point. Unfortunately, starting with N! configurations to examine was idea killing. After an hour or two trying without success to use a recursive approach similar to my rooks solution, I stumbled upon a linear time solution– painting the board with knight-moves!

The pattern was simple — start from one square to the right of the bottom-left corner and place a queen. Move two squares to the right and one up (AKA a “knight’s move”) and place another. Repeat across the board. After getting to the right edge, wrap around to the left side and continue.

The only problem is that this pattern doesn’t work for all board sizes. It only works for boards of size N where N mod 6 is not 2 or 3.

Amazingly, this technique of solving N-Queens is faster than just printing the chess board! While the size of the board is N^2, this solution traverses only N squares– those on which it places a queen.

### The set of all answers for N-Queens

Solving for all queens is a pretty intimidating problem.Unlike N-Rooks, which has N! solutions and must take N! time, there are far fewer solutions for N-Queens. Using the set of all solutions for N-Rooks and then eliminating solutions which collide on the diagonals would be a straight-forward solution, but by using N-Rooks as a starting point it would take N! time. While, I haven’t found a similar recursive solution for N-Queens, it feels like there’s a polynomial solution out there. There just has to be :D. Alas, our class at Hack Reactor only had 2 days of working on the problem before it was time to move on to another project.

### More fun in the console.

If you’re interested in playing around a bit with my program, open your JS console with Firebug or cmd-option-j in Chrome and look at the functions directly. Try `solveNRooks(3, {'all':true});` and inspect the returned object to get a look how things work.

This entry was posted in Uncategorized. Bookmark the permalink.