SlideShare a Scribd company logo
1 of 15
Download to read offline
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

More from vikas dhakane

Ai lecture 12(unit03)
Ai lecture  12(unit03)Ai lecture  12(unit03)
Ai lecture 12(unit03)vikas dhakane
 
Ai lecture 11(unit03)
Ai lecture  11(unit03)Ai lecture  11(unit03)
Ai lecture 11(unit03)vikas dhakane
 
Ai lecture 11(unit03)
Ai lecture  11(unit03)Ai lecture  11(unit03)
Ai lecture 11(unit03)vikas dhakane
 
Ai lecture 10(unit03)
Ai lecture  10(unit03)Ai lecture  10(unit03)
Ai lecture 10(unit03)vikas dhakane
 
Ai lecture 10(unit03)
Ai lecture  10(unit03)Ai lecture  10(unit03)
Ai lecture 10(unit03)vikas dhakane
 
Ai lecture 09(unit03)
Ai lecture  09(unit03)Ai lecture  09(unit03)
Ai lecture 09(unit03)vikas dhakane
 
Ai lecture 07(unit03)
Ai lecture  07(unit03)Ai lecture  07(unit03)
Ai lecture 07(unit03)vikas dhakane
 
Ai lecture 05(unit03)
Ai lecture  05(unit03)Ai lecture  05(unit03)
Ai lecture 05(unit03)vikas dhakane
 
Ai lecture 05(unit03)
Ai lecture  05(unit03)Ai lecture  05(unit03)
Ai lecture 05(unit03)vikas dhakane
 
Ai lecture 04(unit03)
Ai lecture  04(unit03)Ai lecture  04(unit03)
Ai lecture 04(unit03)vikas dhakane
 
Ai lecture 04(unit03)
Ai lecture  04(unit03)Ai lecture  04(unit03)
Ai lecture 04(unit03)vikas dhakane
 
Ai lecture 03(unit03)
Ai lecture  03(unit03)Ai lecture  03(unit03)
Ai lecture 03(unit03)vikas dhakane
 
Ai lecture 03(unit03)
Ai lecture  03(unit03)Ai lecture  03(unit03)
Ai lecture 03(unit03)vikas dhakane
 
Ai lecture 003(unit03)
Ai lecture  003(unit03)Ai lecture  003(unit03)
Ai lecture 003(unit03)vikas dhakane
 
Ai lecture 003(unit03)
Ai lecture  003(unit03)Ai lecture  003(unit03)
Ai lecture 003(unit03)vikas dhakane
 
Ai lecture 02(unit03)
Ai lecture  02(unit03)Ai lecture  02(unit03)
Ai lecture 02(unit03)vikas dhakane
 
Ai lecture 01(unit03)
Ai lecture  01(unit03)Ai lecture  01(unit03)
Ai lecture 01(unit03)vikas dhakane
 
Ai lecture 01(unit03)
Ai lecture  01(unit03)Ai lecture  01(unit03)
Ai lecture 01(unit03)vikas dhakane
 
Ai lecture 02(unit03)
Ai lecture  02(unit03)Ai lecture  02(unit03)
Ai lecture 02(unit03)vikas dhakane
 
Ai lecture 06(unit-02)
Ai lecture 06(unit-02)Ai lecture 06(unit-02)
Ai lecture 06(unit-02)vikas dhakane
 

More from vikas dhakane (20)

Ai lecture 12(unit03)
Ai lecture  12(unit03)Ai lecture  12(unit03)
Ai lecture 12(unit03)
 
Ai lecture 11(unit03)
Ai lecture  11(unit03)Ai lecture  11(unit03)
Ai lecture 11(unit03)
 
Ai lecture 11(unit03)
Ai lecture  11(unit03)Ai lecture  11(unit03)
Ai lecture 11(unit03)
 
Ai lecture 10(unit03)
Ai lecture  10(unit03)Ai lecture  10(unit03)
Ai lecture 10(unit03)
 
Ai lecture 10(unit03)
Ai lecture  10(unit03)Ai lecture  10(unit03)
Ai lecture 10(unit03)
 
Ai lecture 09(unit03)
Ai lecture  09(unit03)Ai lecture  09(unit03)
Ai lecture 09(unit03)
 
Ai lecture 07(unit03)
Ai lecture  07(unit03)Ai lecture  07(unit03)
Ai lecture 07(unit03)
 
Ai lecture 05(unit03)
Ai lecture  05(unit03)Ai lecture  05(unit03)
Ai lecture 05(unit03)
 
Ai lecture 05(unit03)
Ai lecture  05(unit03)Ai lecture  05(unit03)
Ai lecture 05(unit03)
 
Ai lecture 04(unit03)
Ai lecture  04(unit03)Ai lecture  04(unit03)
Ai lecture 04(unit03)
 
Ai lecture 04(unit03)
Ai lecture  04(unit03)Ai lecture  04(unit03)
Ai lecture 04(unit03)
 
Ai lecture 03(unit03)
Ai lecture  03(unit03)Ai lecture  03(unit03)
Ai lecture 03(unit03)
 
Ai lecture 03(unit03)
Ai lecture  03(unit03)Ai lecture  03(unit03)
Ai lecture 03(unit03)
 
Ai lecture 003(unit03)
Ai lecture  003(unit03)Ai lecture  003(unit03)
Ai lecture 003(unit03)
 
Ai lecture 003(unit03)
Ai lecture  003(unit03)Ai lecture  003(unit03)
Ai lecture 003(unit03)
 
Ai lecture 02(unit03)
Ai lecture  02(unit03)Ai lecture  02(unit03)
Ai lecture 02(unit03)
 
Ai lecture 01(unit03)
Ai lecture  01(unit03)Ai lecture  01(unit03)
Ai lecture 01(unit03)
 
Ai lecture 01(unit03)
Ai lecture  01(unit03)Ai lecture  01(unit03)
Ai lecture 01(unit03)
 
Ai lecture 02(unit03)
Ai lecture  02(unit03)Ai lecture  02(unit03)
Ai lecture 02(unit03)
 
Ai lecture 06(unit-02)
Ai lecture 06(unit-02)Ai lecture 06(unit-02)
Ai lecture 06(unit-02)
 

Recently uploaded

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 

Recently uploaded (20)

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
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