SlideShare a Scribd company logo
1 of 9
N QUEENS PROBLEM IN C USING
BACKTRACKING
• N Queens Problem is a famous puzzle in which n-
queens are to be placed on a N x N chess board
• No two queens are in the same row, same column
or same diagonal.
• Sharing the C program to find solution for N
number of Queens problem using backtracking.
Below animations show the solution for 8 queens
problem using the backtracking.
8x8 CHESS BOARD
PROGRAM
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
print f(" - N Queens Problem Using Backtracking -");
print f("nn Enter number of Queens:");
scan f("%d",&n);
queen(1,n);
return 0;
}
void print(int n)
{
int i,j;
printf("nnSolution %d:nn",++count);
for(i=1;i<=n;++i)
printf("t%d",i);
for(i=1;i<=n;++i)
{
printf("nn%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("tQ"); //queen at i,j position
else
printf("t-"); //empty slot
}
}
}
int place(int row,int column)
{
int i;
for(i=1;i<=row-1;++i)
{
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 1; //no conflicts
}
void queen(int row,int n)
{
int column;
for(column=1;column<=n;++column)
{
if(place(row, column))
{
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}}
OUTPUT

More Related Content

What's hot

What's hot (18)

[程式設計]Blackfriday
[程式設計]Blackfriday[程式設計]Blackfriday
[程式設計]Blackfriday
 
Progr2
Progr2Progr2
Progr2
 
Properties real numbers basic
Properties real numbers basicProperties real numbers basic
Properties real numbers basic
 
Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]
 
Graph Plots in Matlab
Graph Plots in MatlabGraph Plots in Matlab
Graph Plots in Matlab
 
Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Eso3 tablasestadisticas blog
Eso3 tablasestadisticas blogEso3 tablasestadisticas blog
Eso3 tablasestadisticas blog
 
Flood Filling Algorithm in C
Flood Filling Algorithm in CFlood Filling Algorithm in C
Flood Filling Algorithm in C
 
Boundary Fill Algorithm in C
Boundary Fill Algorithm in CBoundary Fill Algorithm in C
Boundary Fill Algorithm in C
 
Eso3 tablasestadisticas blog
Eso3 tablasestadisticas blogEso3 tablasestadisticas blog
Eso3 tablasestadisticas blog
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
Skill7addition within 18
Skill7addition within 18Skill7addition within 18
Skill7addition within 18
 
C coding#1
C   coding#1C   coding#1
C coding#1
 
Square of an Input Number - Digital Logic Design | Lecture 5
Square of an Input Number - Digital Logic Design | Lecture 5Square of an Input Number - Digital Logic Design | Lecture 5
Square of an Input Number - Digital Logic Design | Lecture 5
 
201707 SER332 Lecture 17
201707 SER332 Lecture 17   201707 SER332 Lecture 17
201707 SER332 Lecture 17
 
Program to reflecta triangle
Program to reflecta triangleProgram to reflecta triangle
Program to reflecta triangle
 

Similar to Puzzle in c program

N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfN Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfakashreddy966699
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bPhilip Schwarz
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Philip Schwarz
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxtristans3
 
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdfImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdfgopalk44
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical ComputingNaveed Rehman
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptxsunidhi740916
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysisVishal Singh
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassensVikash Dhal
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSKavyaSharma65
 

Similar to Puzzle in c program (20)

N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfN Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
C-programs
C-programsC-programs
C-programs
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
2 d matrices
2 d matrices2 d matrices
2 d matrices
 
Codeware
CodewareCodeware
Codeware
 
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdfImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Discrete time signals on MATLAB
Discrete time signals on MATLABDiscrete time signals on MATLAB
Discrete time signals on MATLAB
 
Discrete time signals on MATLAB
Discrete time signals on MATLABDiscrete time signals on MATLAB
Discrete time signals on MATLAB
 
CS.3.Arrays.pdf
CS.3.Arrays.pdfCS.3.Arrays.pdf
CS.3.Arrays.pdf
 
8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx8 QUEENS PROBLEM.pptx
8 QUEENS PROBLEM.pptx
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassens
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
2D array
2D array2D array
2D array
 

Recently uploaded

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 

Recently uploaded (20)

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 

Puzzle in c program

  • 1.
  • 2.
  • 3. N QUEENS PROBLEM IN C USING BACKTRACKING • N Queens Problem is a famous puzzle in which n- queens are to be placed on a N x N chess board • No two queens are in the same row, same column or same diagonal. • Sharing the C program to find solution for N number of Queens problem using backtracking. Below animations show the solution for 8 queens problem using the backtracking.
  • 5. PROGRAM #include<stdio.h> #include<math.h> int board[20],count; int main() { int n,i,j; void queen(int row,int n); print f(" - N Queens Problem Using Backtracking -"); print f("nn Enter number of Queens:"); scan f("%d",&n); queen(1,n); return 0; }
  • 6. void print(int n) { int i,j; printf("nnSolution %d:nn",++count); for(i=1;i<=n;++i) printf("t%d",i); for(i=1;i<=n;++i) { printf("nn%d",i); for(j=1;j<=n;++j) //for nxn board { if(board[i]==j) printf("tQ"); //queen at i,j position else
  • 7. printf("t-"); //empty slot } } } int place(int row,int column) { int i; for(i=1;i<=row-1;++i) { if(board[i]==column) return 0; else if(abs(board[i]-column)==abs(i-row))
  • 8. return 1; //no conflicts } void queen(int row,int n) { int column; for(column=1;column<=n;++column) { if(place(row, column)) { board[row]=column; //no conflicts so place queen if(row==n) //dead end print(n); //printing the board configuration else //try queen with next position queen(row+1,n); } }}