SlideShare a Scribd company logo
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
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
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
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
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
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
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
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
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
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…
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
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
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
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
Ai lecture  10(unit02)

More Related Content

PDF
Pc 4.2 notes
PPTX
N queens using backtracking
PPTX
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
PPTX
PUZZLE IN C PROGRAMMING
PPTX
Puzzle in c program
PPTX
Backtracking Basics.pptx
PPTX
N Queen Algorithm
PDF
Constraint Satisfaction Problem (CSP) by A z m jalal uddin joy_v1.0.1
Pc 4.2 notes
N queens using backtracking
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
PUZZLE IN C PROGRAMMING
Puzzle in c program
Backtracking Basics.pptx
N Queen Algorithm
Constraint Satisfaction Problem (CSP) by A z m jalal uddin joy_v1.0.1

Similar to Ai lecture 10(unit02) (20)

PPTX
8 QUEENS PROBLEM.pptx
PPTX
8 queen problem
PPTX
The n Queen Problem
PPTX
N-queens.pptx
PPTX
The N-Queens problemdskksnjfnskjdfnsjnddjsdnjs
PPTX
N queen puzzle
PPTX
Backtracking Algorithm.pptx
PDF
N Queen Problem
PPTX
proficiency presenattion on topic of backtracking algo
PPTX
N queen problem
PDF
N queens problem
PPTX
Lecture2b algorithm
PPTX
backtracking 8 Queen.pptx
PPT
Backtracking Algorithm.ppt
PPTX
Backtrack-search-algorithm (2).pptx
PPTX
8-Queens Problem.pptx
PDF
module5_backtrackingnbranchnbound_2022.pdf
PDF
N Queens problem
PPTX
Branch and bound
PPTX
data structures- back tracking
8 QUEENS PROBLEM.pptx
8 queen problem
The n Queen Problem
N-queens.pptx
The N-Queens problemdskksnjfnskjdfnsjnddjsdnjs
N queen puzzle
Backtracking Algorithm.pptx
N Queen Problem
proficiency presenattion on topic of backtracking algo
N queen problem
N queens problem
Lecture2b algorithm
backtracking 8 Queen.pptx
Backtracking Algorithm.ppt
Backtrack-search-algorithm (2).pptx
8-Queens Problem.pptx
module5_backtrackingnbranchnbound_2022.pdf
N Queens problem
Branch and bound
data structures- back tracking
Ad

More from vikas dhakane (20)

PDF
Ai lecture 14(unit03)
PPTX
Ai lecture 13(unit03)
PDF
Ai lecture 13(unit03)
PPTX
Ai lecture 12(unit03)
PDF
Ai lecture 12(unit03)
PPTX
Ai lecture 11(unit03)
PDF
Ai lecture 11(unit03)
PPTX
Ai lecture 10(unit03)
PDF
Ai lecture 10(unit03)
PDF
Ai lecture 09(unit03)
PDF
Ai lecture 07(unit03)
PPTX
Ai lecture 05(unit03)
PDF
Ai lecture 05(unit03)
PPTX
Ai lecture 04(unit03)
PDF
Ai lecture 04(unit03)
PPTX
Ai lecture 03(unit03)
PDF
Ai lecture 03(unit03)
PPTX
Ai lecture 003(unit03)
PDF
Ai lecture 003(unit03)
PPTX
Ai lecture 02(unit03)
Ai lecture 14(unit03)
Ai lecture 13(unit03)
Ai lecture 13(unit03)
Ai lecture 12(unit03)
Ai lecture 12(unit03)
Ai lecture 11(unit03)
Ai lecture 11(unit03)
Ai lecture 10(unit03)
Ai lecture 10(unit03)
Ai lecture 09(unit03)
Ai lecture 07(unit03)
Ai lecture 05(unit03)
Ai lecture 05(unit03)
Ai lecture 04(unit03)
Ai lecture 04(unit03)
Ai lecture 03(unit03)
Ai lecture 03(unit03)
Ai lecture 003(unit03)
Ai lecture 003(unit03)
Ai lecture 02(unit03)
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Welding lecture in detail for understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Digital Logic Computer Design lecture notes
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
R24 SURVEYING LAB MANUAL for civil enggi
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
bas. eng. economics group 4 presentation 1.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CH1 Production IntroductoryConcepts.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
additive manufacturing of ss316l using mig welding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Lecture Notes Electrical Wiring System Components
Welding lecture in detail for understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Digital Logic Computer Design lecture notes
Operating System & Kernel Study Guide-1 - converted.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS

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