8 Queens
Chapter: 8 Queens
Backtracking with a stack works very well for small magic square problems. But the exponential nature of backtracking algorithms quickly renders the
solution of larger squares intractable. You probably had enough trouble solving the 4*4, let alone the 5*5.
Here is another classical puzzle: Try to place eight queens on a chessboard
so that no queen attacks any other. Queens can move any number of spaces
along their own row, their own column, or either of the two diagonals
that pass through them. The applet below has been deliberately slowed down
using
try {
Thread.sleep(DELAY);
}
catch (InterruptedException ie) {}
after each update. But still it should have a solution by the time
you finish reading this paragraph. You may be interested to know that
DELAY is set to half a second, so the algorithm itself is pretty
fast on an 8*8 chessboard.
Initially you might have thought this would be a hopelessly slow search
involving 64 possible positions (the squares of the chessboard) each with
two possibilities (queen or no-queen). This would lead you to fear that
a backtracking program would have to search among 2^648^8