8 Queens Backtrack

download 8 Queens Backtrack

of 15

Transcript of 8 Queens Backtrack

  • 7/28/2019 8 Queens Backtrack

    1/15

    The 8 Queens problem

    By

    R. Kishore Kumar

  • 7/28/2019 8 Queens Backtrack

    2/15

    Objective of 8 Queen problem

    Considering an n*n chessboard and try to find

    all ways to place n nonattacking queens.

  • 7/28/2019 8 Queens Backtrack

    3/15

    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.

  • 7/28/2019 8 Queens Backtrack

    4/15

    Example of a backtracking solution to

    4-queen problem

    1 1

    . . 2

    1

    2

    . . . .

    1

    2

    3

    . . . .

    1

    2

    3

    1 1

    . . . 2

    1

    2

    3

    . . 4

  • 7/28/2019 8 Queens Backtrack

    5/15

    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.

  • 7/28/2019 8 Queens Backtrack

    6/15

    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 samerow + column value.

  • 7/28/2019 8 Queens Backtrack

    7/15

    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|

    Example of 8 Queen problem cont.

  • 7/28/2019 8 Queens Backtrack

    8/15

    1

    2

    3

    4

    5

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

    1

    2

    3 4

    5

    6

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

    Solution of 8-Queen

  • 7/28/2019 8 Queens Backtrack

    9/15

    1

    2

    3

    4

    6 5

    (8,6,4,2,1,1,1) = 1401

    1

    2

    3

    4

    5

    (8,6,4,3,2) = 1977

    1

    2

    3

    4

    5

    6

    7

    8

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

    Solution of 8-Queen cont.

  • 7/28/2019 8 Queens Backtrack

    10/15

    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.

  • 7/28/2019 8 Queens Backtrack

    11/15

    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

  • 7/28/2019 8 Queens Backtrack

    12/15

    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

  • 7/28/2019 8 Queens Backtrack

    13/15

    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.

  • 7/28/2019 8 Queens Backtrack

    14/15

    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).

    Algorithm explanation cont.

  • 7/28/2019 8 Queens Backtrack

    15/15

    THANK YOU!!!