Category Archives: Articles

Recursion: N-Queen Problem

Know a bit about the Queen piece in a game of Chess

A solution for the “8 queens” problem

Numberphile video:

In this exercise, you will solve the classic 8-queens problem: place 8 queens on an 8-by-8 chess board so that no two queens are in the same row, column, or diagonal.

There are 8! = 40,320 ways in which no two queens are placed in the same row or column.
Any permutation p[] of the integers 0 to 7 gives such a placement: put queen i in row i, column p[i].
Your program Queens_YI.java should take an integer command-line argument n and enumerate all solutions to the n-queens problem by drawing the location of the queens in ASCII like the two solutions below.

* * * Q * * * *      * * * * Q * * * 
* Q * * * * * *      * Q * * * * * * 
* * * * * * Q *      * * * Q * * * * 
* * Q * * * * *      * * * * * * Q * 
* * * * * Q * *      * * Q * * * * * 
* * * * * * * Q      * * * * * * * Q 
* * * * Q * * *      * * * * * Q * * 
Q * * * * * * *      Q * * * * * * * 

Hint: to determine whether setting
q[n] = i conflicts with q[0] through q[n-1]
if q[i] equals q[n]: two queens are placed in the same column
if q[i] – q[n] equals n – i: two queens are on same major diagonal
if q[n] – q[i] equals n – i: two queens are on same minor diagonal

WARNING: do not look for a solution onlilne.

https://math.stackexchange.com/questions/1872444/how-many-solutions-are-there-to-an-n-by-n-queens-problem

http://www.brainmetrix.com/8-queens/