Sei sulla pagina 1di 15

The 8 Queens problem

By R. Kishore Kumar

Objective of 8 Queen problem


Considering an n*n chessboard and try to find all ways to place n nonattacking queens.

Understanding 4 Queen problem


Here (X1,..,Xn) represent a solution in which Xi is the column of the ith row where ith queen is placed. The Xis will all be distinct since no two queens can be placed in the same column.

Example of a backtracking solution to 4-queen problem


1 1 . . 2 . . 1 2 . . 3 1 2

1 2 3 . . . .

1 .

1 . . 2 3 .

1 2 . 4

8-Queen problem
Chessboards squares being numbered as the indices of two dimensional array a[1:n][1:n]. We can observe that every element on the same diagonal that runs from upper left to lower right has the same row-column value.

Example of 8 Queen problem


Consider the queen at a[4][2] The square that are diagonal to this queen are a[3][1], a[5][3], a[6][4], a[7][5] and a[8][6]. All these squares have row-column value of 2. Every element on the same diagonal that goes from the upper right to lower left has the same row + column value.

Example of 8 Queen problem cont.


Two queens are placed at position (i,j) and (k,l) then by the above they are on same diagonal only if i - j = k - l or i + j = k + l The first equation implies j - l = i - k Two queens lie on the same diagonal if and only if |j-l| = |i-k|

Solution of 8-Queen
1 2 3 4 5 6 3 4 5 1 2

(8,5,4,3,2) = 1649

(8,5,3,1,2,1) = 769

Solution of 8-Queen cont.


1 2 3 4 6 5 4 5 5 1 2 3 3 4 1

6
7 8 (8,6,4,2,1,1,1) = 1401 (8,6,4,3,2) = 1977 (8,5,3,2,2,1,1,1) = 2329

Algorithm for NQueen


Place(k,i) returns a boolean value that is true if the kth queen can be placed in column i. It test both whether i is distinct from all previous values X[1],,X[k-1] and also whether there is no other queen on the same diagonal.

Bool Place(int k, int i) //Returns true if a queen can be placed in kth row //and ith column. Otherwise it returns false. x[] //is a global array whose first (k-1) values have //been set. Abs(r) returns the absolute value of r { for(int j=1; j<k; j++) if((x[j]==i)//Two in the same column || (abs(x[j]-i)==abs(j-k))) //or in the same diagonal return(false); return(true); }

void NQueens(int k,int n) //Using backtracking,this procedure prints all //possible placements of n queens on an nxn //chessboard so that they are nonattacking { for(int i=1;i<n;i++) { if(Place(k,i)) { x[k]=i; if(k==n) { for(int j=1;j<=n;j++) cout<<x[j]<<' ': cout<<endl; } else NQueens(k+1,n); } } }

Algorithm explanation
For an 8*8 chessboard there are (648) possible ways to place 8 pieces. Placement of each queen on the chessboard was chosen randomly. With each choice we keep track of number of columns a queen could legitimately be placed on.

Algorithm explanation cont.


The vector represents the value that function estimate would produce from these sizes. The average of these five trials is 1625. The total number of nodes in the 8-queens state space tree is 69,281= 1 +
7 =0[ =(8

)]

Its computational time is O(k-1).

THANK YOU!!!

Potrebbero piacerti anche