Problem Decomposition: Goal Trees, Rule Based Systems, Rule Based Expert Systems. Planning:
STRIPS, Forward and Backward State Space Planning, Goal Stack Planning, Plan Space Planning,
A Unified Framework For Planning. Constraint Satisfaction : N-Queens, Constraint Propagation,
Scene Labeling, Higher order and Directional Consistencies, Backtracking and Look ahead
Strategies.
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
Ai lecture 10(unit02)
1. Topic To Be Covered:
Example of CSP:N-Queens Problem Solution
Using Backtracking
Jagdamba Education Society's
SND College of Engineering & Research Centre
Department of Computer Engineering
SUBJECT: Artificial Intelligence & Robotics
Lecture No-10(UNIT-02)
Prof.Dhakane Vikas N
2. N-Queens Problem Solution Using Backtracking
What is N-Queen Problem?
This problem is to find an arrangement of N queens on a chess board,
such that no queen can attack any other queens on the board.
The N Queen is the problem of placing N chess queens on an N×N
chessboard so that no two queens attack each other.
Queen can attack each other if they are in same column, row & diagonal.
Problem Statement: We have to place 4-Queens on 4 x 4 chessboard such
that no queen will attack each other(For that we have to check no two
queens are placed in same Row, Column & Diagonal
3. N-Queens Problem Solution Using Backtracking
Example :4 x 4 Chessboard To place 4-
Queens
Step:-1
r=0,c=0
Check, board[0][0] =safe
Now ,Increment column By 1(Place Queen)
Step:2
r=0,c=1
Check, board[0][1] ≠ safe
Now ,Increment Row By 1
Step:3
r=1,c=1
Check, board[1][1] ≠ safe
Now ,Increment Row By 1
Q
Q
Q
4. N-Queens Problem Solution Using Backtracking
Example :4 x 4 Chessboard To place 4-
Queens
Step:-4
r=2,c=1
Check, board[2][1] =safe
Now ,Increment column By 1(Place Queen)
Step:5
r=0,c=2
Check, board[0][2] ≠ safe
Now ,Increment Row By 1
Step:6
r=1,c=2
Check, board[1][2] ≠ safe
Now ,Increment Row By 1
Q
Q
Q
Q
Q
Q
5. N-Queens Problem Solution Using Backtracking
Example :4 x 4 Chessboard To place 4-Queens
Step:-7
r=2,c=2
Check, board[2][2] ≠ safe
Now ,Increment Row By 1
Step:8
r=3,c=2
Check, board[3][2] ≠ safe
Now ,Increment Row By 1
Now after this if we try to increment Row by 1 it
becomes 4..which is invalid, as we have 4*4
chessboard here, so here we have reach to END of
Board.. and we could not able to placed 4 queens so,
here we have to BACKTRACKKKKK…
Q
Q
Q
Q
6. N-Queens Problem Solution Using Backtracking
Example :4 x 4 Chessboard To place 4-Queens
Note: When u backtrack we have to remove last
placed queen and increment ROW BY 1
So ,here at position Board[2][1] we have lastly
placed the queen so ,we remove it increment ROW
BY 1…So we get next step as follows…
Step:9
r=3,c=1
Check, board[3][1] =safe
Now ,Increment Column By 1(placed Queen)
Step:10
r=0,c=2
Check, board[0][2] ≠ safe
Now ,Increment Row By 1
Q
Q
Q
Q
7. N-Queens Problem Solution Using Backtracking
Step:11
r=1,c=2
Check, board[1][2] =safe
Now ,Increment Column By 1(placed queen)
Step:12
r=0,c=3
Check, board[0][3] ≠ safe
Now ,Increment Row By 1
Step:13
r=1,c=3
Check, board[1][3] ≠ safe
Now ,Increment Row By 1
Q
Q
Q
Q
Q
Q
Q
Q
Q
8. N-Queens Problem Solution Using Backtracking
Step:14
r=2,c=3
Check, board[2][3] ≠ safe
Now ,Increment Row By 1
Step:15
r=3,c=3
Check, board[3][3] ≠ safe
Now ,Increment Row By 1
Now after this if we try to increment Row by 1 it
becomes 4..which is invalid, as we have 4*4
chessboard here, so here we have reach to END of
Board.. and we could not able to placed 4 queens
so, here we have to BACKTRACKKKKK…
Q
Q
Q
Q
Q
Q
9. N-Queens Problem Solution Using Backtracking
Note: When u backtrack we have to remove last
placed queen and increment ROW BY 1
So ,here at position board[1][2] we have lastly
placed the queen so ,we remove it increment ROW
BY 1…So we get next step as follows…
Step:16
r=2,c=2
Check, board[2][2] ≠ safe
Now ,Increment Row By 1
Step:17
r=3,c=2
Check, board[3][2] ≠ safe
Now ,Increment Row By 1
Q
Q
Q
Q
10. N-Queens Problem Solution Using Backtracking
Now after this if we try to increment Row by 1 it becomes 4..which is
invalid, as we have 4*4 chessboard here, so here we have reach to END
of Board.. and we could not able to placed 4 queens so, here we have to
BACKTRACKKKKK…
Note: When u backtrack we have to remove last placed queen and
increment ROW BY 1
So ,here at position board[3][1] we have lastly placed the queen so ,we
remove it increment ROW BY 1
Now after this if we try to increment Row by 1 it becomes 4..which is
invalid, as we have 4*4 chessboard here, so here we have reach to END
of Board.. and we could not able to placed 4 queens so, here we have to
BACKTRACKKKKK…
11. N-Queens Problem Solution Using Backtracking
Note: When u backtrack we have to remove last
placed queen and increment ROW BY 1
So ,here at position board[0][0] we have lastly
placed the queen so ,we remove it increment ROW
BY 1…So we get next step as follows…
Step:18
r=1,c=0
Check, board[1][0] = safe
Now ,Increment column By 1
Step:19
r=0,c=1
Check, board[0][1] ≠ safe
Now ,Increment Row By 1
Q
Q
12. N-Queens Problem Solution Using Backtracking
Step:20
r=1,c=1
Check, board[1][1] ≠ safe
Now ,Increment Row By 1
Step:21
r=2,c=1
Check, board[2][1] ≠ safe
Now ,Increment Row By 1
Step:22
r=3,c=1
Check, board[3][1] = safe
Now ,Increment Column By 1
Q
Q
Q
Q
13. N-Queens Problem Solution Using Backtracking
Step:23
r=0,c=2
Check, board[0][2] =safe
Now ,Increment Column By 1
Step:24
r=0,c=3
Check, board[3][0] ≠ safe
Now ,Increment Row By 1
Step:25
r=1,c=3
Check, board[1][3] ≠ safe
Now ,Increment Row By 1
Q
Q
Q
Q
Q
Q
Q
Q
Q
14. N-Queens Problem Solution Using Backtracking
Step:26
r=2,c=3
Check, board[2][3] =safe
Now ,Increment Column By 1
So, finally we have placed all 4 queen
Q
Q
Q
Q