SlideShare a Scribd company logo
Puzzle in c program
Puzzle in c program
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

PPTX
10 mavzu(prezent)
PPTX
Factoring Strategies
PPTX
C programming codes for the class assignment
PDF
Computer Graphics in Java and Scala - Part 1b
PDF
Notes and guide for matlab coding and excersie
PDF
Computer Graphics in Java and Scala - Part 1
DOC
C-programs
DOCX
In C Programming create a program that converts a number from decimal.docx
10 mavzu(prezent)
Factoring Strategies
C programming codes for the class assignment
Computer Graphics in Java and Scala - Part 1b
Notes and guide for matlab coding and excersie
Computer Graphics in Java and Scala - Part 1
C-programs
In C Programming create a program that converts a number from decimal.docx

Similar to Puzzle in c program (20)

PPTX
Algorithm Homework Help
PDF
C Programming Example
DOCX
2 d matrices
PDF
Codeware
PDF
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
PDF
MATLAB for Technical Computing
PPT
All important c programby makhan kumbhkar
PDF
Discrete time signals on MATLAB
PDF
Discrete time signals on MATLAB
PDF
CS.3.Arrays.pdf
PPTX
8 QUEENS PROBLEM.pptx
PDF
Numerical analysis
PDF
DOCX
Implementation of strassens
PPTX
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
PDF
LET US C (5th EDITION) CHAPTER 2 ANSWERS
PDF
2D array
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
PPTX
Cse 121 presentation on matrix [autosaved]
DOCX
Algorithm Homework Help
C Programming Example
2 d matrices
Codeware
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
MATLAB for Technical Computing
All important c programby makhan kumbhkar
Discrete time signals on MATLAB
Discrete time signals on MATLAB
CS.3.Arrays.pdf
8 QUEENS PROBLEM.pptx
Numerical analysis
Implementation of strassens
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
LET US C (5th EDITION) CHAPTER 2 ANSWERS
2D array
Chapter 4 : Balagurusamy Programming ANSI in C
Cse 121 presentation on matrix [autosaved]
Ad

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
master seminar digital applications in india
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Institutional Correction lecture only . . .
PDF
Pre independence Education in Inndia.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Computing-Curriculum for Schools in Ghana
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Structure & Organelles in detailed.
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
master seminar digital applications in india
Pharma ospi slides which help in ospi learning
Institutional Correction lecture only . . .
Pre independence Education in Inndia.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
GDM (1) (1).pptx small presentation for students
Computing-Curriculum for Schools in Ghana
Basic Mud Logging Guide for educational purpose
Pharmacology of Heart Failure /Pharmacotherapy of CHF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial disease of the cardiovascular and lymphatic systems
VCE English Exam - Section C Student Revision Booklet
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
Supply Chain Operations Speaking Notes -ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Structure & Organelles in detailed.
Ad

Puzzle in c program

  • 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); } }}