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

More from vikas dhakane

Ai lecture 12(unit03)
Ai lecture  12(unit03)Ai lecture  12(unit03)
Ai lecture 12(unit03)
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
 

More from vikas dhakane (20)

Ai lecture 12(unit03)
Ai lecture  12(unit03)Ai lecture  12(unit03)
Ai lecture 12(unit03)
 
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)
 

Recently uploaded

Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
TaghreedAltamimi
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
SakkaravarthiShanmug
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
shahdabdulbaset
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 

Recently uploaded (20)

Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 

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