#include < stdio.h>
#include < conio.h>
#include < math.h>
int board[20];
int count;
void print_board(int);
void Queen(int,int);
int place(int,int);
void main()
{
int n,i,j;
clrscr();
printf("\n-------------------------------------------------------");
printf("\n n-Queen problem using Backtracking");
printf("\n-------------------------------------------------------");
printf("\n Enter number of Queen:");
scanf("%d",&n);
Queen(1,n);
}
void print_board(int n)
{
int i,j;
printf("\n-------------Solution %d---------------\n ",++count);
for(i=1;i < =n;i++)
{
printf(" %d",i);
}
printf("\n-------------------------------------------------------");
for(i=1;i < =n;i++)
{
printf("\n%2d | ",i);
for(j=1;j < =n;j++)
{
if(board[i]==j)
printf(" Q");
else
printf(" -");
}
}
printf("\n-------------------------------------------------------");
printf("\n Press any key to continue....");
getch();
}
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 0;
}
return 1;
}
void Queen(int row,int n)
{
int column;
for(column=1;column < =n;column++)
{
if(place(row,column))
{
board[row]=column;
if(row==n)
print_board(n);
else
Queen(row+1,n);
}
}
}
OUTPUT
-------------------------------------------------------
n-Queen problem using Backtracking
-------------------------------------------------------
Enter number of Queen:4
-------------Solution 1---------------
1 2 3 4
-------------------------------------------------------
1 | - Q - -
2 | - - - Q
3 | Q - - -
4 | - - Q -
-------------------------------------------------------
Press any key to continue....
-------------Solution 2---------------
1 2 3 4
-------------------------------------------------------
1 | - - Q -
2 | Q - - -
3 | - - - Q
4 | - Q - -
-------------------------------------------------------
Press any key to continue....
#include < conio.h>
#include < math.h>
int board[20];
int count;
void print_board(int);
void Queen(int,int);
int place(int,int);
void main()
{
int n,i,j;
clrscr();
printf("\n-------------------------------------------------------");
printf("\n n-Queen problem using Backtracking");
printf("\n-------------------------------------------------------");
printf("\n Enter number of Queen:");
scanf("%d",&n);
Queen(1,n);
}
void print_board(int n)
{
int i,j;
printf("\n-------------Solution %d---------------\n ",++count);
for(i=1;i < =n;i++)
{
printf(" %d",i);
}
printf("\n-------------------------------------------------------");
for(i=1;i < =n;i++)
{
printf("\n%2d | ",i);
for(j=1;j < =n;j++)
{
if(board[i]==j)
printf(" Q");
else
printf(" -");
}
}
printf("\n-------------------------------------------------------");
printf("\n Press any key to continue....");
getch();
}
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 0;
}
return 1;
}
void Queen(int row,int n)
{
int column;
for(column=1;column < =n;column++)
{
if(place(row,column))
{
board[row]=column;
if(row==n)
print_board(n);
else
Queen(row+1,n);
}
}
}
OUTPUT
-------------------------------------------------------
n-Queen problem using Backtracking
-------------------------------------------------------
Enter number of Queen:4
-------------Solution 1---------------
1 2 3 4
-------------------------------------------------------
1 | - Q - -
2 | - - - Q
3 | Q - - -
4 | - - Q -
-------------------------------------------------------
Press any key to continue....
-------------Solution 2---------------
1 2 3 4
-------------------------------------------------------
1 | - - Q -
2 | Q - - -
3 | - - - Q
4 | - Q - -
-------------------------------------------------------
Press any key to continue....