#include<stdio.h>
#include<stdlib.h>
void nqueens(int,int);
int place(int,int);
void printsolution(int,int[]);
int x[10],count=0;

void printsolution(int n,int x[])
{
 int i,j;
 char c[10][10];
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  c[i][j]='X';
 }
 for(i=1;i<=n;i++)
 c[i][x[i]]='Q';
        printf("Solution %d\n\n",++count);
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   printf("%c\t",c[i][j]);
  }
  printf("\n");
 }
}


int place(int k,int i)
{
 int j;
 for(j=1;j<k;j++)
 {
  if((x[j]==i)||(abs(x[j]-i)==abs(j-k)))
  return 0;
 }
 
 return 1;
}


void nqueens(int k,int n)
{

 int i;
 for(i=1;i<=n;i++)
 {
  if(place(k,i)==1)
  {
   x[k]=i; 
   if(k==n)
   printsolution(n,x);
   else
   nqueens(k+1,n);
  }
 }
}


void main()
{
       int n;
        printf("\n enter no of queens");
        scanf("%d",&n);
 nqueens(1,n);
 

}
/*
Output
ProfessionalCipher @www.professionalcipher.blogspot.com:~$ cd Desktop
ProfessionalCipher @www.professionalcipher.blogspot.com:~/Desktop$ cd print
ProfessionalCipher @www.professionalcipher.blogspot.com:~/Desktop/print$ ls
a.out      nqueen.c~                 st.c              str.c~
assTemp.c  outputfile.txt            st.c~             test1.txt
FILE1.ASM  pass.c                    strass.c          test1.txt~
floyd.c    pipe5a.c                  strassen_d.c      warshall.c
floyd.c~   pipe5a.c~                 strassen_d.c~     warshall.c~
hufheap.c  printit3rdasignofsl2.zip  strassens_method
nqueen.c   rqueen.c                  str.c
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ gcc rqueen.c
ProfessionalCipher@www.professionalcipher.blogspot.com:~/Desktop/print$ ./a.out

 enter no of queens5
Solution 1

Q X X X X 
X X Q X X 
X X X X Q 
X Q X X X 
X X X Q X 
Solution 2

Q X X X X 
X X X Q X 
X Q X X X 
X X X X Q 
X X Q X X 
Solution 3

X Q X X X 
X X X Q X 
Q X X X X 
X X Q X X 
X X X X Q 
Solution 4

X Q X X X 
X X X X Q 
X X Q X X 
Q X X X X 
X X X Q X 
Solution 5

X X Q X X 
Q X X X X 
X X X Q X 
X Q X X X 
X X X X Q 
Solution 6

X X Q X X 
X X X X Q 
X Q X X X 
X X X Q X 
Q X X X X 
Solution 7

X X X Q X 
Q X X X X 
X X Q X X 
X X X X Q 
X Q X X X 
Solution 8

X X X Q X 
X Q X X X 
X X X X Q 
X X Q X X 
Q X X X X 
Solution 9

X X X X Q 
X Q X X X 
X X X Q X 
Q X X X X 
X X Q X X 
Solution 10

X X X X Q 
X X Q X X 
Q X X X X 
X X X Q X 
X Q X X X 
ProfessionalCipher @www.professionalcipher.blogspot.com:~/Desktop/print$ 

*/