vlad furash & steven wine. problem surfaced in 1848 by chess player max bezzel as 8 queens...
TRANSCRIPT
Vlad Furash & Steven Wine
Problem surfaced in 1848 by chess player Max Bezzel as 8 queens (regulation board size)
Premise is to place N queens on N x N board so that they are non-attacking
A queen is one of six different chess pieces It can move forward & back, side to side
and to its diagonal and anti-diagonals The problem: given N, find all
solutions of queen sets and return either the number of solutions and/or the patterned boards.
1) Generate a list of free cells on the next row.
› If no free cells, backtrack (step 2 of previous row)
2) Place a queen on next free cell & proceed with step 1 for next row
› If no next row, proceed to step 3
3) At this point, you have filled all rows, so count/store as a solution
F F F F F Q
Q
F F F
Q
Q
Q
Q
F
Q
Q
Q
Q
Q
Q
F
Q
Q
Q
Q
Q
Q
Q
Q
F
Q
Q
Q
Q
Q
DONE
Further look ahead—not just next row› Keep track of total free spaces per row
If any row ahead has a zero count, backtrack Jump to other rows
› To prevent unnecessary future backtracks, jump to rows with fewest free spots and place queens there first
Cell blocking› Prevention of placing queens on cells that
would lead to repeating of previously found solution and/or putting the board in an unsolvable situation
5 F F F F F
5 F F F F F
5 F F F F F
5 F F F F F
5 F F F F F
- Q
2 - - - F F
3 F - F - F
3 F - F F -
4 F - F F F
- Q
- Q
1 F - - - -
2 F - F - -
3 F - F - F
- Q
- Q
- Q
1 - - F - -
1 - - - - F
- Q
- Q
- Q
- Q
- Q
- Q
- Q
- Q
- Q
2 - F - - -- F -
2 - F - - -- - F
0 --
- - -- - -
2 - F - - - F- -
*example of when to backtrackdue to looking ahead (row 7)
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
All possible solutions for N=5
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Only 2 unique solutions for N=5(notice transformations & reflections)
A Line is a set of all squares that make up a row, column, or diagonal. If one of the squares in a line has a queen in it then the line is considered closed.
Examples of lines
There are n(rows) + n(columns) + 4n – 2(diagonals) = 6n – 2 lines on N×N board.
• A Configuration C is a placement of at most n queens on N×N board is considered feasible if no two queens attack each other.
• A completion of a configuration is placement of remaining queens that results in a solution.
Proposition 1: Two feasible configurations with the same number of closed lines contain the same number of queens.
Main Theorem: If two feasible configurations have the same set of closed lines, then completion of one configuration is also a completion of the other configuration.
Perform a breadth first search on the set of feasible configurations, while checking if combining every line set with the next line set yields a line set that has already been found.
After the algorithm iterates sum the counter for every lines set, which contains closed lines for all rows and all columns to find number of solutions.
Set QUEUE = {< ,1∅ >}. QUEUE is a set of structures <S, i>, where S is a set of closed lines and i is the counter for an equivalence class.
For every unexamined square, choose a square and let T be a set of lines containing the square.• For every set <S, i> є QUEUE, s.t. S ∩ T= , DO:∅
If <S U T, j> є QUEUE, for some j, replace j with i + j, Otherwise add <S U T, i> to QUEUE.
Return QUEUE
If there are p possible closed lines, need to store 2p equivalence classes, which is the size of QUEUE. Size of an element in QUEUE is at most O(n2), size of the board. Overall space complexity is O(n22p). Size of p is bounded by 6n – 2. O(n264n)
Running time is also O(n264n), since there are n2
squares and algorithm iterates though at most 2p
equivalence classes.