Data structure Program for NQueen Problem


#include<stdio.h>
void main()
{
int i,k=0,n,row[15],ddiag[20],udiag[20];
int board[15],r,j;
clrscr();
printf("\n\t\tN QUEEN'S PROBLEM\n\n");
printf("\n\tEnter the No. of Queens to be placed : ");
scanf("%d",&n);
for(i=0;i<n;i++)
board[i]=row[i]=0;
for(i=0;i<(2*n);i++)
ddiag[i]=udiag[i]=0;
while(1)
{
r=board[k];
if(!row[r] && !ddiag[n-1-k+r] && !udiag[k+r])
{
row[r]=ddiag[n-1-k+r]=udiag[k+r]=1;
k++;
if(k==n)
{
printf("\n\n\t\tSOLUTION\n\n ");
for(i=0;i<n;i++,printf("\n\n "))
for(j=0;j<n;j++,printf(" "))
if(j==board[i])
printf("Q");
else
printf("-");
getch();
exit(0);
}
else
board[k]=0;
}
else
{
board[k]++;
while(board[k]==n)
{
k--;
row[board[k]]=0;
ddiag[n-1-k+board[k]]=0;
udiag[k+board[k]]=0;
board[k]++;
}
}
}
}