Вы находитесь на странице: 1из 3

Eight Queens Algorithm

The Eight Queens Algorithm strives to set up an 8x8 chess board such that eight queens can be
placed on the board and not hit each other, each having the full capabilities of a queen (can move
unlimited spaces vertically, horizontally, and diagonally). There are 4,426,165,368 possible arrangements
and 92 solutions (on an 8x8 chessboard). These 92 solutions also include solutions that are symmetrical or
reflected in some way on the board. If eliminating solutions that reflect/mirror each other, the algorithm
comes up with 12 solutions, called fundamental solutions.

EXAMPLE OF A SUCCESSFUL EIGHT QUEENS ARRANGEMENT

In my solution, I used the backtracking algorithm to find my eight queens solutions. In the end,
my program was able to generate 92. Alternatively, the iterative repair algorithm also works, in which the
board begins with all queens on the board. In this algorithm, the number of potential attacks on each of
the queens is calculated and calculations are conducted used to better place the queens. Instead, I used the
backtracking algorithm, in which a queen would be initially placed in the upper-right corner and queens
would be added to the top of each column, traveling down the rows until a legal place is found. If the
queen reaches the end of the board, meaning there are no legal placements available, the queens backtrack
and the previous column continues down the row until a legal placement is found. This is done
recursively in the overloaded addQueens() method.

Initially, I was going to use a boolean matrix to keep track of the queens, setting all empty
elements to false and those that contained a queen being set to true. This was initially done without
creating a Queen class. I decided to make a Queen class, which came with also making an ArrayList of
Queens correctly placed, keeping track of their location using integer fields (row and col). This was
initially done on top of the boolean matrix, which was redundant and did not work. I got rid of the
boolean matrix and instead used an ArrayList of ArrayLists of Queens to keep track of all possible
solutions. Initially, the program only kept track of the solutions with the first queen at (0,0) only, but then
I added a for loop to account for all of the solutions, resulting in 92. This was then applied to the graphics
by setting the boolean field in the Queen.java class to true.
Bibliography

“Eight Queens Puzzle.” Wikipedia, Wikimedia Foundation, 11 May 2018,

en.wikipedia.org/wiki/Eight_queens_puzzle.

“The N by N Queens Problem.” Glossary of Computer Related Terms,

www.math.utah.edu/~alfeld/queens/queens.html.

“N Queen Problem Using Recursive Backtracking.” Code Pumpkin, 15 Mar. 2018,

codepumpkin.com/n-queen-problem/.

Numberphile. “The 8 Queen Problem - Numberphile.” YouTube, YouTube, 21 Aug. 2015,

www.youtube.com/watch?v=jPcBU0Z2Hj8.

Вам также может понравиться