n queen puzzle backtracking algorithm 1. starting from first row, loop through all the positions in...

3
N Queen Puzzle https://en.wikipedia.org/wiki/ Eight_queens_puzzle Backtracking Algorithm 1. Starting from first row, loop through all the positions in this row, if could find a safe position, mark it and move forward to next row. 2. If you can a safe position in the next row, keep moving forward. If this finally leads to a solution, save it. Otherwise transverse back to previous row to look for other type Board struct { field [][]int} type Boards []Board http://www.geeksforgeeks.org/backtracking-set-3-n-queen- problem/ Shuo Zhao [email protected]

Upload: amberlynn-carson

Post on 18-Jan-2016

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: N Queen Puzzle  Backtracking Algorithm 1. Starting from first row, loop through all the positions in this

N Queen Puzzle

https://en.wikipedia.org/wiki/Eight_queens_puzzle

Backtracking Algorithm1. Starting from first row, loop through all the positions in this row, if could find a safe position, mark it and move forward to next row.2. If you can a safe position in the next row, keep moving forward. If this finally leads to a solution, save it. Otherwise transverse back to previous row to look for other possible safe positions3. Repeat until we have looped through all positions

type Board struct {field [][]int}type Boards []Board

http://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/

Shuo [email protected]

Page 2: N Queen Puzzle  Backtracking Algorithm 1. Starting from first row, loop through all the positions in this

• Independent Solutions

N=4 Independent Solutions

N=4 Solutions

N=5, 6, 7, 8 Solutions

N 1 2 3 4 5 6 7 8 9

Ind. 1 0 0 1 2 1 6 12 46

All 1 0 0 2 10 4 40 92 352

https://en.wikipedia.org/wiki/Eight_queens_puzzle

Page 3: N Queen Puzzle  Backtracking Algorithm 1. Starting from first row, loop through all the positions in this

• Nonstandard BoardShape standard column Donut

Attacking conditions x=i; y=j;abs(x-i)=abs(j-y)

x=i; y=j;abs(x-i)=abs(j-y)abs(x-i)=abs(j+n-y)abs(x-i)=abs(j-n-y)

x=i; y=j;abs(x-i)=abs(j-y)abs(x-i)=abs(j+n-y)abs(x-i)=abs(j-n-y)abs(x+n-i)=abs(j-y) abs(x-n-i)=abs(j-y)

• Higher Dimensional Board (3d as example)

abs(x-i)=abs(y-j)=abs(z-k) (3dimensional diagonal)if x=i (yz plane): y=j; z=k; (1dimensional); abs(y-j)=abs(z-k) (2dimensional)if y=i (xz plane): x=i; z=k; (1dimensional); abs(x-i)=abs(z-k) (2dimensional)if z=k (xy plane): y=j; x=i; (1dimensional); abs(y-j)=abs(x-i) (2dimensional) if x!=i&& y!=i&& z!=k (xyz space): abs(x-i)=abs(y-j)=abs(z-k) (3dimensional)

Jeremiah Barr and Shrisha Rao, Department of Computer Science, Mount Mercy College, IA 52402 http://www.micsymposium.org/mics_2004/Barr.pdf

N=4